Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView appears with empty cells

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!

like image 737
Oleg Avatar asked Feb 21 '23 14:02

Oleg


2 Answers

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;
}
like image 88
adig Avatar answered Mar 04 '23 01:03

adig


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

like image 32
Jpellat Avatar answered Mar 04 '23 01:03

Jpellat