UITableView has cells, but shows no cells - ios

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].

Related

UITableview displays limited cells

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];

UITableView static cell's number of row is changed

I make one UITableView Controller had static cell. And I set number of rows 3 in Storyboard. But rows does not set 3, just be made more and more like this screen shot. I don't touch any programatic code. Did I have to make it programmatically?
That's the normal behavior of a UITableView. Even though you only have 3 rows, the view itself extends to the bottom, and it shows where the cells would be if you had data in them. To fix, do one of two things: customize the UITableView so the dividing line between cells is invisible [UIColor clearColor], or change the size of the UITableView's height depending on how many cells you have.
If you add a footerView to the UITableView then it will not extend all the way to the bottom.
I solve this problem on the story board.
Create one more cell. if you want 3cells, then make 4cells.
Make whatever you want on cell. put the UIButton or UILabel any way. But except 4th cell.
Expend your 4th cell's height, to the bottom.
And finally, check hidden in attributes inspector. It makes 4th cell hidden.
That's it!
And I add one image file. I hope it help your work. Thanks.
Simple solution is to set footer's frame to nil:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/*........*/
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
return cell;
}
I would use a regular View Controller and insert a TableView of the required table height.
Then if you really want to you can do stuff with the cell height and label sizes.

Change view position when configuring cell. Strange behavior

I have a standard UINavigationController with a UITableViewController at it's root. In IB, I paint a prototype cell with a label and a UIView. The UIView contains a button. I'd like the UIView to be x-aligned after the label, as a function of the length of text in the label.
In IB, the view's left side is initially aligned with the label's left side. There are no layout constraints in IB.
Here's my cellForRowAtIndexPath ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray *labels = #[#"SOME STRING", #"SHORTER", #"A VERY MUCH LONGER ONE", #"REGULAR ONE", #"TINY"];
UILabel *label = (UILabel *)[cell viewWithTag:32];
label.text = labels[indexPath.row];
UIView *view = [cell viewWithTag:33];
CGSize size = [label.text sizeWithAttributes:#{NSFontAttributeName:label.font}];
view.frame = CGRectOffset(label.frame, size.width, 0);
return cell;
}
Two problems: I've set breakpoints and watch this code running the first time the view appears. I see the view.frame get changed for each row, but the view does not change position. I remains in it's IB-position, right on top of the label. If I scroll the table down, the views on lower cells (presumably reused) are in the desired position. If I scroll back up, the upper rows are also good. It just fails to work on the initial presentation of the upper cells.
Second problem is that the button contained in only the first row has a subtle, strange effect applied to it's text, like a blur. See attached...
First Row Button (zoomed in mac preview... see that extra blur on the left edge of the letters?)
Other Row Buttons
Stuff I tried:
I've tried a few variations, including using a regular view controller with a table view added (rather than a UITableViewController). I've found that if I reloadData on viewDidAppear, that solves the placement problem, but not the blurry button. (Also, I don't like the idea of needing to reload on viewDidAppear). Doing so on viewWillAppear has no effect at all. I've also tried animating the label change slowly. It happens, but again, only on the second time the cell is configured. I try changing the UIView color to prove the code is being run. The color change happens every time, including the first time, but not the view placement. Am I nuts?
For problem 1:
Try calling [cell layoutIfNeeded] before returning the cell, there should be no performance hit when it does not need relayout.
For problem 2:
try calling CGRectIntegral before you set the frame. ie
view.frame = CGRectIntegral( CGRectOffset(label.frame, size.width, 0) );

Prototype Cell not being used by tableview

I have read many posts about similar problems but nothing seems to work, I am obviously doing something wrong. I have a TableViewController that is in a StoryBoard (XCode 5). For the PrototypeCell I set the type to custom and set the Identifier to "pbvcell". I added some labels, changed the background etc.. Here is my tableview delegate method for setting the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"pbvcell"];
// Configure the cell...
PBVlead *lead = [self.leads objectAtIndex:indexPath.row];
NSLog(#"Cell class %#", [cell class]);
UILabel *leadNameLabel = (UILabel *)[cell viewWithTag:1];
leadNameLabel.text = lead.leadName;
return cell;
}
Now the app launches but even after I add an object to the tableview datasource array and do a reload data, the cells are blank, like the custom cell is not being used. It looks like this should be easy and thats all I need to do. What on earth am I missing?
I left this in a comment above but just to keep things tidy I will post it as an answer here. It is silly but it is good to note that you have to manipulate the contextView of a prototype cell and not the tableview cell itself in order for your visual changes to have an effect...
"Because I embrace my own stupidity I will tell everyone what was going on here. I had set the TableCell background to blue and added some UILabels and set there color to white to show up against the blue background. Run the app, no labels.... What I finally realized is, I had not set the Content View background to blue. So..... What was happening was the labels actually are shown in the content view in the view hierarchy. White labels on a white background equals, invisible... :-) I set the content view background to blue and wola, there is everything! :-) Brother..?

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.

Resources