gpt4 book ai didi

ios - 从 xib 加载 uitableviewcell 的 View 与 dequeuereusablecellwithidentifier 方法有何不同?

转载 作者:行者123 更新时间:2023-11-28 07:05:33 27 4
gpt4 key购买 nike

我在两个不同的 View Controller 中有两个表。在某些时候,两个表可能具有相同类型的单元格。由于这两个表位于不同的 View Controller 中,如果我使用 dequeueReusableCellWithIdentifier 方法获取单元格,我将不得不在两个表中有两个不同的单元格但包含相同类型的数据。

另一方面,如果我将 xib 用于 tableViewCell 的 View ,我可以在两个 Controller 中重用它。

我知道使用 dequeueReusableCellWithIdentifier 给我带来了性能优势。我想知道当我改用 xib 时对性能有何影响。

最佳答案

当您显示表格 View 时,每个单元格都在即将显示时加载。 dequeueReusableCellWithIdentifier 允许您获取刚离开屏幕的单元格并将其再次用于下一个将出现的单元格。就像回收垃圾一样。

它是这样工作的:

-(void)viewDidLoad{
...
[self.tableView registerNib:[UINib nibWithNibName:@"nibname"
bundle:nil]
forCellReuseIdentifier:@"CustomCellReuse"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Starting from iOS 5, the following will either dequeue an existing cell or instantiate a new one from xib
static NSString *CellIdentifier = @"CustomCellReuse";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
return cell;
}

如果您不重用您的单元格,您将在大 TableView 中遇到严重的性能问题。

但是您不能在不同的表格 View 中“重复使用”单元格。你必须为它们都实现之前的方法。

关于ios - 从 xib 加载 uitableviewcell 的 View 与 dequeuereusablecellwithidentifier 方法有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com