UITableview displays limited cells - ios

I have a UITableView that is setup correct and at some point I wanted
to display only a limited amount of cells. Now that I want to revert back
the functionality I am unable to do so.
The property that limits the visible cells (according to my git log):
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
After removing the above line, I still do not get the desired effect.
But something was introduced, and I am not sure what to search for.
I want the default behavior back with many empty cells.
Here is my methods for the sections and data, currently I only have
two cells visible.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
View Hierarchy Debugging data.
I am adding images of the View Debugger, "Wire Frame View" and "Content View" seperately.
From the images you will see two types of separator lines;
I mimicked a line by adding space at the bottom of the cell
so that I could have "thin lines".

Please check the tableview height. I think height of the table currently you are seeing is less so you are getting only 2 visible cells.

As one can see from the attached images, the wire frames show that the separators are in fact drawn but they are not visibly on the screen when you view the content.
Simply change the colors:
self.tableView.backgroundColor = [UIColor redColor];
self.tableView.separatorColor = [UIColor greenColor];

Related

Stop UISearchDisplayController from showing empty cells

I've implemented a UISearchDisplayController on a fairly standard tableview (same datasource for table + search). The problem I am having is when the results don't fill the screen there are "pseudo" rows below the actual results.
I was able to set the background color, but can't find a good way to suppress these rows/separators. They seem decoupled from my numberOfRowsInSection: delegate response.
If I set the searchResultsTableView.separatorColor (green) it only changes the actual results rows.
I was able to change separatorStyle to UITableViewCellSeparatorNone, but then I have to manually recreate the separators on the actual results and there are edge cases (like the selection color covers up my view).
Is there a clean way to hide the rows pointed out in the attached screenshot?
You can probably implement this delegate method for the Search Display Controller:
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView;
{
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 1)];
footer.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:footer];
}
This will make those last few rows disappear. You can of course do this in any other method you choose. For example, you can do this in viewDidLoad of a UIViewController if you want the same effect on a normal UITableView.

Background Texture of a grouped UITableView won't scroll with the table

I'm working on an app that has lots of UITableViews and I'm trying to give them a textured background color. I need to use the Grouped style because I don't want the section headers to float over the text fields when the user scrolls.
The problem I'm having is that when I use the Grouped style, the background texture doesn't scroll with the table; it stays in place while the table scrolls above it. I feel like this is kind of weird and I would rather have the background scroll with the table, the way it does in the Plain style. Unfortunately, because I can't have the header views floating on top of everything, that doesn't appear to be an option.
Has anyone been able to accomplish this?
Here's some relevant code:
- (void)loadView {
[super loadView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texturedPattern.png"]];
// this prevents the cells from replicating the background texture
self.tableView.backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// disable the default grouped border since we're doing it manually with the textField
cell.backgroundView = nil;
cell.backgroundColor = [UIColor clearColor];
}
And my table view currently looks like this:
UPDATE
As Amit Vyawahare suggested, I tried applying the background texture to the background of the headers and each cell. There are two problems that are both much more obvious when you see it in motion, but I'll do my best.
First, the background is always visible. I've removed the background color from the table to make it more obvious:
Everywhere you see black, the background texture of the tableView would be visible and it will not scroll with the tableView. The Grouped tableView style inserts the 5 pixel border on either side of every cell and can't be changed. Additionally, there is no footer beneath the Staff ID section, and I've even implemented -tableView:heightForFooterInSection: to return 0.0, but there's still a gap there.
Second, even if I were able to get rid of these gaps, the textures don't line up anyway. Again, this is difficult to see, so I've uploaded a retina screen shot to make it a little easier:
This is most obvious above the Password section, you can see the textures don't align properly and it looks kind of like a "fold" in the paper. Which would be cool, I guess, if that's what the client wanted. It's visible, but less obvious on just about every edge from the second screen shot. This is because the texture is actually quite large: 200x200 (400x400#2x), and there are slight variations in color that aren't noticeable unless this sort of misalignment happens.
First replace your UITableViewController by a UIViewController and add a UITableView to it. Set the autoresizingMask to flexible width/height. Now you have something equivalent to a UITableViewController, but with more control over the view hierarchy.
Then add a view below the tableview (actually: add that one first) which holds the background.
Finally, set the delegate of the scrollview to your class, and respond to scroll events by updating your background view accordingly.

UITableView has cells, but shows no cells

So, the correct number of rows shows up. On pressing any row, the correct action takes place. However, the cells themselves are nowhere to be seen.
I added a NSTimer in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for each cell just to trace it out beyond the function - they all say that their superview is equal to the tableview in question (and is not nil, so i'm not checking nil == nil or something).
They all contain labels with the correct text.
The separator lines are being drawn.... If I change the TableView background, the whole visible area's background shows as that color.
I'm checking that each cell is neither hidden nor set to an alpha of 0.
Is there anything else I could be missing?
Are you loading from your cells from a nib file or creating programmatically?
Are you overlaying another object over your cell in the cell subview? Perhaps a subview is covering it; I can't tell, since you have not posted any code yet. Given the information you have provided, it is difficult to determine why you cannot see the cells backgroundView.
Try changing the color with
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
tmpView.backgroundColor = [UIColor blackColor];
myCell.backgroundView = tmpView;
It sounds like you have set the backgroundView of your cell to [UIColor clearColor].

Cell background incorrectly repeating in UITableViewCell when scrolling

I have a full list that populates a UITableView. This I want to background the one of the cells with a different color and it works initially, but for some reason when I start scrolling the table up and down, it starts drawing more cells with the green background.
Please note that there is always one detailCell.detailTimeLabel.text that's equal to currentTime.
The code I have is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... SOME MORE CODE HERE ...
if ([detailCell.detailTimeLabel.text isEqualToString:currentTime]) {
UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backgroundView.backgroundColor = [UIColor greenColor];
detailCell.backgroundView = backgroundView;
for (UIView* view in detailCell.contentView.subviews)
{
view.backgroundColor = [UIColor clearColor];
}
}
}
Which can be the problem?
Your trying to store data in a tableviewcell. You can't do this because the cells are constantly reused. The background you see repeated occurs because its the same cell being displayed over and over again with different text.
When you dequeue the cell, you need to reset it to blank and wipe out all the previous data. Then you should set the background color only if the data you are putting into the cell has the current time.
As a general rule, you should never refer to data in the cells. If you need to know what is in a particular cell, look at the data at the indexpath within the datamodel itself.

UITableView separator lines disappear between cells on scroll

Problem: The separator between cells in a table view appear only for those cells shown when the view loads, and only at load time. When the tableview is scrolled down, the cells scrolled into view show no separator between them, then when the tableview is scrolled back up, the initial cells show no separator.
Details: I've got a UITableView to which I'm adding standard UITableViewCells. These cells are created with initWithFrame, frame height = 90px. I'm adding a custom view created from a nib to this cell's view, height = 90px. The cell height is specified at 90px in tableView:heightForRowAtIndexPath:.
Has anyone experienced this behavior?
I had a feeling the solution to this would be simple...
I made the height of my cells 91px and the separator lines appear as they should on scroll.
I couldn't use Douglas's solution because my tables have a huge amount of cells and would become pretty much unusable on older phone. Reusing cells is key for performance.
BUT, I managed to workaround the problem using a transparent separator and adding my own in the contentView of the cell, as follows:
yourTable.separatorColor = [UIColor clearColor];
separatorView.frame = FactRectMake(0, rowHeight-1, appFrame.size.width, 0.2);
I had the same problem, but I used a different solution.
My separators were disappearing because I was clearing my cell using:
for (UIView *eachView in self.subviews) {
[eachView removeFromSuperview];
}
This removed the separator view as well!
Instead, I assigned a tag for each of my customs views (three labels) right before adding them to the sub view:
tempFirstNameLabel.tag = 100;
self.firstNameLabel = tempFirstNameLabel;
[self addSubview:self.firstNameLabel];
Then when I cleared the cell, I just removed those views:
for (int i = 100; i<103; i++) {
UIView *eachView = [self viewWithTag:i];
[eachView removeFromSuperview];
}
Hope this helps!
This also avoids the memory management issues that #Douglas Smith's solution posed.
You should set separator none and then single line again
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// it is a bug in iOS 7
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

Resources