【问题标题】:UITableView appears with empty cellsUITableView 出现空单元格
【发布时间】:2012-05-15 20:05:31
【问题描述】:

我在显示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;
}

提前感谢您的建议!

【问题讨论】:

    标签: ios objective-c xcode uitableview cocoa-touch


    【解决方案1】:

    好吧,您有在创建单元格之前发生的单元格自定义代码。

    改成这样:

    - (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;
    }
    

    【讨论】:

      【解决方案2】:

      问题是你做的

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

      之前

      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }
      

      改变顺序。发生的情况是,第一次执行表视图时,您从未在分配之前执行标题。当您重用单元格时,它会起作用,因为单元格!= nil

      【讨论】:

      • 感谢您的解释!你是对的,这就是问题所在!所有的人都发布了有效的答案,所以我只接受第一个
      • 好的,谢谢。总是认为你在分配它之后没有确定有效的单元格。当您重用一个单元格时,如果他找不到任何可用的单元格,它可能会返回 nil,这肯定会在第一次显示表格时发生。当您遇到与刷新或其他类似问题相关的问题时,请观察您的代码部分。一定会找到解决办法的!
      【解决方案3】:

      你需要把这些行

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

      if(){...} 条件之后。

      在第一遍中,单元格为零。将这些行放在if 之前什么都不做。这就是您看到空单元格的原因。

      一个简单的问题

      你为什么打电话给[self.filesList retain]

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-23
        • 2016-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多