【问题标题】:UITableViewCells not getting reused when scrolling滚动时 UITableViewCells 没有被重用
【发布时间】:2014-02-01 05:17:09
【问题描述】:

我正在构建一个应用程序,其中屏幕应该显示一个表格视图,其中表格视图的前两行是两个不同的自定义单元格,其余的是不同的自定义单元格。所以我在我的 cellForRowAtIndexPath 中做了这个: 我的代码:

if (indexPath.row == 0 && !isSearchEnabled) {
         FirstRowViewCell *cell;
        static NSString *CellIdentifier = @"FirstCell";

       cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            // load a new cell from the nib file
            [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];
            cell = self.firstCell;
            //cell.delegate = self;
            //cell.indexPath = indexPath;
            self.firstCell = nil;

        }
        return cell;
    } else if (indexPath.row == 1 && !isSearchEnabled) {
        SecondRowViewCell *cell;
        static NSString *CellIdentifier = @"SecondCell";

        cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            // load a new cell from the nib file
            [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];
            cell = self.secondCell;
            //cell.delegate = self;
            //cell.indexPath = indexPath;
            self.secondCell = nil;

        }
        return cell;
    }else {

    static NSString *CellIdentifier = @"ContactCell";

    ContactTableCell *cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {


          [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];

        // load a new cell from the nib file

        cell = self.contactCell;
        cell.delegate = self;
        cell.indexPath = indexPath;
        self.contactCell = nil;

    }
    return cell;}

但在滚动表格视图时它会滞后。当我在仪器中使用 Time Profiler 检查时,它显示 cellForRowAtIndexPath 尤其是

[[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];

加载需要很长时间,这会导致延迟。我的问题是,单元格是否被重用,因为即使我们滚动到顶部,上面的行也会为每个单元格执行。请帮助我解决滞后问题。谢谢!

【问题讨论】:

    标签: ios iphone objective-c uitableview


    【解决方案1】:

    试试这个可能会有帮助

     static NSString *simpleTableIdentifier = @"ContactCell";
    
    ContactCell *cell = (ContactCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    
    
    }         
    
    return cell;
    

    【讨论】:

      【解决方案2】:

      您正在创建没有设置reuseIdentifier 的单元格。 reuseIdentifierUITableViewCell 的只读属性。你应该做的是将笔尖注册到 tableview,也许在你的 UIViewControllerviewDidLoadviewWillAppear 方法中。

      UINib *cellNib = [UINib nibWithNibName:@"ContactCell" bundle:nil];
      [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
      

      一旦您这样做了,dequeueReusableCellWithIdentifier: 将始终返回一个有效的单元格,无论是实例化它还是为您重用它。

      您可以参考以下 Apple 文档:UITableViewUITableViewCellUINib

      【讨论】:

      • 所以我应该采取这一行 [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];超出 cellForRowAtIndexPath?
      • @AndrewsJ 是的,一旦你使用了registerNib:forCellReuseIdentifier:,你就不再需要loadNibNamed:owner:options:。只要您之前为该标识符注册了一个 nib,dequeueReusableCellWithIdentifier: 将始终返回一个有效单元格。
      猜你喜欢
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多