我在显示UITableView时出现问题:某些单元格为空,单元格中的内容仅在滚动后才会显示(如果空单元格滚出屏幕然后返回).我无法理解可能是什么问题.
这是我的代码:
- (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; }
提前感谢您的建议!
解决方法
好吧,您有在创建单元格之前发生的单元格自定义代码.
像这样改变它:
- (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; }