我正在向cellForRowIndexPath中的单元格添加项目(例如手势识别器,子视图).如果重复使用单元格(大概是),我不想添加这些,所以有没有办法轻松判断单元格是新的还是被重用?
单元原型在故事板中定义.
我没有为单元格使用自定义子类(看起来像是矫枉过正).我正在使用单元格标记来识别子视图,因此无法使用它.
我可以使用iOS 6之前的方法,但肯定有更好的方法来做这么简单的事情吗?
我在网上找不到任何东西,所以害怕我可能会对某些东西感到困惑 - 但搜索起来很困难.
解决这个问题的最简单方法是检查是否存在需要添加的内容.
因此,假设您的单元格需要具有标记42的子视图(如果它尚未存在).
- (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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1790 次 |
| 最近记录: |