UITableView appears with empty cells

I have a problem with displaying a UITableView : some cells are empty, and the contents in the cells only become visible after scrolling (if an empty cell scrolls from the screen and then returns). I do not understand what could 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; } 

Thanks in advance for your suggestions!

+4
source share
3 answers

Well, you have a cell setup code that happens before the cell is created.

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; } 
+4
source

The problem is what you do

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

reorder. What happens is that the first time you start the View table, you never make a header before alloc. When you reuse a cell, it works because the cell! = Nil

+3
source

You need to put these lines

 [cell.imageView setImage:[UIImage imageNamed:@"oie_png-1.png"]]; cell.textLabel.text = title; 

after the condition if(){...} .

In the first pass, the cells are zero. Put these lines before if does nothing. This is why you see empty cells.

Simple question

Why are you calling [self.filesList retain] ?

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1411073/


All Articles