OGeek|极客世界-中国程序员成长平台

标题: ios - 为行数有限的 UITableView 禁用虚拟化(单元重用) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:34
标题: ios - 为行数有限的 UITableView 禁用虚拟化(单元重用)

我有一个行数有限的 UITableView(比如说 20-30)。 是否可以禁用单元重用?

Most of solutions建议不要调用dequeueReusableCellWithIdentifier。 但在这种情况下,每次 UITableViewSource 需要新单元格时 - 都会创建该单元格的新实例。

我想要的是,一旦我一直向下滚动并看到所有 20-30 个单元格,在返回的路上不应创建新单元格。

有可能吗?



Best Answer-推荐答案


即使您使用唯一标识符,也不能指望重用池大小足以存储您尝试执行的 20-30 个唯一单元格。

您需要在数组或字典中保存自己对单元格的引用,并使用它来获取 cellForRowAtIndexPath 中的单元格 - 例如 -

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    NSString *cellIdentifier=[NSString stringWithFormat"Cell%d",indexPath.row];

    UITableViewCell *cell=self.cellDictionary[cellIdentifier];

    if (cell == nil) {
          cell=[[MyCell alloc]init];  // Do whatever is required to allocate and initialise your cell
          self.cellDictionary[cellIdentifier]=cell;
    }

    // Any other cell customisation

    return cell;
}

除非创建单元非常昂贵,否则重复使用通常是更好的方法

关于ios - 为行数有限的 UITableView 禁用虚拟化(单元重用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329332/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4