gpt4 book ai didi

ios - 如何确定 dequeueReusableCellWithIdentifier 返回的单元格是否在 iOS 6 中被重用?

转载 作者:可可西里 更新时间:2023-11-01 03:47:34 26 4
gpt4 key购买 nike

我正在向 cellForRowIndexPath 中的单元格添加项目(例如手势识别器、 subview )。如果单元格被重复使用(大概),我不想添加这些,那么有没有一种方法可以轻松判断单元格是新的还是正在重复使用?

单元格原型(prototype)在 Storyboard中定义。

我没有为单元格使用自定义子类(看起来有点矫枉过正)。我正在使用单元格标签来识别 subview ,所以不能使用它。

我可以使用 iOS 6 之前的方法,但肯定有更好的方法来完成如此简单的事情?

我在网上找不到任何东西,所以担心我可能对某些东西感到困惑 - 但很难搜索。

最佳答案

解决此问题的最简单方法是检查是否存在您需要添加的内容。

假设您的单元格需要有一个带有标签 42 的 subview ,如果它不存在的话。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
UIView *subview = [cell viewWithTag:42];
if (!subview) {
... Set up the new cell
}
else {
... Reuse the cell
}
return cell;
}

关于ios - 如何确定 dequeueReusableCellWithIdentifier 返回的单元格是否在 iOS 6 中被重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14902144/

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