UITableViewCell - 加载25个单元格 - 不重复使用

viv*_*nha 2 iphone uitableview

我有25个表格单元格,我想将所有这些单元格加载在一起,而不重复使用它们.

任何的想法?

Tyl*_*ler 5

如果你想避免回收表格单元格,你可以调用避免dequeueReusableCellWithIdentifier:在你的tableView:cellForRowAtIndexPath:方法.

如果要对所有单元格进行一次性初始加载,可以在表格的数据源的init方法中执行以下操作:

// myCellArray is an instance var of type NSMutableArray.
myCellArray = [NSMutableArray new];
for (int i = 0; i < 25; ++i) {
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  UITableViewCell *cell = [self tableView:tV cellForRowAtIndexPath:indexPath];
  [myCellArray addObject:cell];
}
Run Code Online (Sandbox Code Playgroud)

这会将细胞保留在记忆中,因为它们会被myCellArray你保留.

为了提高效率,您的cellForRowAtIndexPath:方法可以是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  if ([myCellArray count] > indexPath.row) {
    return [myCellArray objectAtIndex:indexPath.row];
  }
  return [self createCellAtRow:indexPath.row];
}
Run Code Online (Sandbox Code Playgroud)

但是,您需要小心使用比您认为需要的更多内存,并避免花费太多时间初始化表.在许多情况下,如果您只是以标准方式使用回收的单元格,您的应用程序可能会显得更快并且使用更少的内存(例如,建议在UITableViewDataSource文档中).