I have a problem displaying UITableView
: Some cells are empty, and content in cells becomes visible only after scrolling (if empty cell scrolls out of the screen and then goes back). I Can't understand what might be the problem.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateFilesList];
[tableView reloadData];
}
- (void) viewDidAppear:(BOOL)animated
{
animated = YES;
[self updateFilesList];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filesList retain];
NSString *title = [self.filesList objectAtIndex:indexPath.row];
title = [title stringByDeletingPathExtension];
title = [title lastPathComponent];
if (title.length >33) {
title = [title substringFromIndex:33];
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.imageView setImage:[UIImage imageNamed:@"oie_png-1.png"]];
cell.textLabel.text = title;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
Thank you in advance for your suggestions!
Well, you have the cell customization code that happens before you create the cell.
Change it like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filesList retain];
NSString *title = [self.filesList objectAtIndex:indexPath.row];
title = [title stringByDeletingPathExtension];
title = [title lastPathComponent];
if (title.length >33) {
title = [title substringFromIndex:33];
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// This part creates the cell for the firs time
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// This part customizes the cells
[cell.imageView setImage:[UIImage imageNamed:@"oie_png-1.png"]];
cell.textLabel.text = title;
return cell;
}
The problem is that you make
[cell.imageView setImage:[UIImage imageNamed:@"oie_png-1.png"]];
cell.textLabel.text = title;
before
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
change the order. What is happening is that the first time you execute the table View you never do the title before the alloc. When you reuse cell it works because the cell != nil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With