为选定的单元格禁用 prepareForReuse [英] Disable prepareForReuse for selected cell

查看:29
本文介绍了为选定的单元格禁用 prepareForReuse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 UITableViewCell.当一个单元格被选中时,一个 UILabel 被添加到其中.我不得不使用 prepareForReuse 以免弄乱,就像这样:

I have a custom UITableViewCell. When a cell gets selected, a UILabel gets added to it. I had to use prepareForReuse for it not to get messy, like so:

- (void)prepareForReuse {
    NSArray *viewsToRemove = [self.view subviews];
    for (UILablel *label in viewsToRemove) {
        [label removeFromSuperview];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategorieCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    return customCell;
}

问题是当我向下滚动到标签不在视野范围内,然后再向上滚动时,标签不再存在.原因很明显是因为当单元格被重复使用时,我删除了所有标签.

The problem is when I scroll down enough that the label is out of view, and then I scroll back up, the label isn't there anymore. The reason is obviously because when the cells get reused, I removed all the labels.

那么有没有办法禁用所选行的 prepareForReuse(或仅方法中的代码),以及如何禁用?

So is there a way to disable prepareForReuse (or just the code in the method) for the selected row, and how?

推荐答案

将表格单元格视为可重复使用以容纳不同内容(标签、图像、按钮等)的愚蠢容器.

Think of table cells as dumb containers that get reused to hold different things (labels, images, buttons, etc.).

您填充 cellForRowAtIndexPath 中的单元格.

You fill the cells in cellForRowAtIndexPath.

您在 prepareForReuse 中清空它们,以便它们可以再次填充和重复使用.

You empty them in prepareForReuse so they can be filled again and reused.

不要混淆这两个动作.

当您填充单元格时,您应该使用存储在其他地方的数据填充它 - 即不是来自其他单元格.如果您在填充单元格时依靠 indexPathsOfSelectedCells 来帮助您,那么您将遇到问题.不要这样做.

When you fill the cell, you should be filling it from data that you have stored somewhere else - i.e. not from other cells. If you are relying on indexPathsOfSelectedCells to help you when filling your cell, you are going to have problems. Don't do this.

通常您会有一个对象数组,其中每个对象对应一个单元格.表格中的单元格数量与数组中的对象数量一样多.您可以在 viewDidLoad 中初始化数组中的对象,或者将它们从先前的视图控制器中传入.

Typically you would have an array of objects, where each object corresponds to a cell. You have as many cells in your table as objects in the array. You might initialize the objects in your array in viewDidLoad, or pass them in from a previous view controller.

这个过程并不复杂.大多数单元格仅显示少量数据,因此您的对象(通常称为模型)不必具有许多属性来保存这些数据.

This process doesn't have to be complicated. Most cells display only a few bits of data, so your object (often called a model) doesn't have to have many properties to hold this data.

当用户选择一个单元格时,在其对应的对象中设置一个selected"属性来表示这一点.即使单元格从屏幕上滚动并重新使用,该值仍会保留在对象中.那很好.

When the user selects a cell, set a "selected" property in its corresponding object to indicate this. This value stays around in the object even when the cell is scrolled off the screen and reused. That's good.

现在,当用户滚动回单元格时,您可以使用来自相应对象的数据填充单元格.由于该对象设置了其selected"属性,因此在这种情况下,您可以通过添加所需的标签来填充"单元格.或者,如果未设置,则不添加标签.

Now when the user scrolls back to the cell, you fill the cell with data from the corresponding object. Since that object has its "selected" property set, you "fill" the cell by adding the label that you want there in this case. Or if it isn't set, you don't add the label.

在 prepareForReuse 中,始终移除标签以使单元格处于空状态,准备重新填充.

In prepareForReuse, always remove the label to put the cell in its empty state, ready to be refilled.

这篇关于为选定的单元格禁用 prepareForReuse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆