【问题标题】:Cell from nib for UITableView preload来自笔尖的单元格用于 UITableView 预加载
【发布时间】:2015-12-23 12:59:58
【问题描述】:

一开始我有这个代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DropDownTableViewCell"];
    if (!cell) {
        NSArray *topLevelObjects =
        [[NSBundle mainBundle] loadNibNamed:@"DropDownView"
                                      owner:self
                                    options:nil];

        for (DropDownTableViewCell *object in topLevelObjects) {
            if ([object class] == [DropDownTableViewCell class]) {
                cell = object;
                break;
            }
        }
        NSAssert(cell, @"Cell must not be nil");
    }

    cell.nameLabel.text = [self.dataSource buttonDownPicker:self stringForRowAtIndexPath:indexPath];

    return cell;
}

当我显示 tableView 和 tableView 单元格开始从 nib 加载时,我的 UI 冻结了几秒钟(由于从 nib 加载每个显示的单元格)。这可以通过提前从 nib 加载单元格来解决:

- (void)awakeFromNib {
   DropDownTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"DropDownView" owner:self options:nil] objectAtIndex:0];       
}

但是这种方式看起来很“hacky”。有没有更合适的解决方案?

根据给出的答案编辑:

我尝试使用 registerNib forCellIdentifier 但它没有加载笔尖,它只是用标识符绑定笔尖,并且在第一次出现 tableView 时所有单元格导致将笔尖加载到内存

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    这根本不是“hacky”。对于从笔尖加载单元格,您通常做的是加载笔尖,然后为您选择的可重用标识符注册单元格。例如,在我的一个项目中,我有一个带有表格视图的视图控制器。在视图中确实加载了我调用:

    [[NSBundle mainBundle] loadNibNamed:@"ChannelListCell" owner:self options:nil];
    [self.channelListTableView registerNib:[UINib nibWithNibName:@"ChannelListCell" bundle:nil] forCellReuseIdentifier:@"channelListCell"];
    

    然后在索引路径处的行的单元格中:

    cell = [tableView dequeueReusableCellWithIdentifier:@"channelListCell" forIndexPath:indexPath];
    

    在您的情况下,出队的单元格始终为零,因为您没有注册它。

    【讨论】:

      【解决方案2】:

      试试这样.....

      - (void)viewDidLoad {
              [super viewDidLoad];
              // Do any additional setup after loading the view, typically from a nib.
      
              UINib *nib = [UINib nibWithNibName:@"" bundle:nil];
              [self.tblview registerNib:nib forCellReuseIdentifier:@"DropDownTableViewCell"];
          }
      
          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
              DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DropDownTableViewCell"];
      
      
              return cell;
          }
      

      【讨论】:

      • 我还没有viewDidLoad,我的班级是一个UIView的孩子。创建冗余笔尖链接与创建冗余单元格相同。我想避免它
      【解决方案3】:

      您可以先在 viewDidLoad 中注册您的课程,如下所示。它变得更快更好

      [self.tableViewObject registerClass: [DropDownTableViewCell class]forCellReuseIdentifier:@"DropDownTableViewCellIdentifier"];
      

      请在您的 DropDownTableViewCell.m 文件中添加以下方法

      -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
          NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DropDownTableViewCell" owner:self options:nil];
              // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
              self = [topLevelObjects objectAtIndex:0];
          return self;
      }
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          static NSString *CellIdentifier=@"DropDownTableViewCellIdentifier";
          DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
      
      //Access all cell properties here.
      
      return cell.
      }
      

      请参考https://stackoverflow.com/a/30954273/914111 以获得更好的知识。

      【讨论】: