gpt4 book ai didi

ios - 使自定义动态单元格在 UITableView 中不可重用

转载 作者:行者123 更新时间:2023-11-29 03:39:28 25 4
gpt4 key购买 nike

我使用的是自定义 UITableViewCell 子类,名为 MessageCell

我像这样初始化单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCell"];

// more code

return cell;

}

在每个自定义单元格中,我都有一个用于标记此单元格的复选框。当单元格被标记时,它将被添加到数组中,反之亦然。

虽然这在数据方面有效,但 UI 并未按应有的方式反射(reflect)它。所发生的情况是,除了标记的单元格之外,其他单元格也被标记。

我认为这种行为源于单元格的可重用性,即当我将单元格标记为选定时,它与此屏幕位置中的所有 future 单元格具有共同的指针。

因此,我想使用“正常”的不可重复使用的单元。

所以我尝试了:

  MessageCell *cell = [[MessageCell alloc] init];

或者:

  MessageCell *cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

但是它们都导致显示空单元格。

最佳答案

您仍然想重复使用单元格!只需检查您的数据并在 cellForRowAtIndexPath 中将复选框设置为选中或取消选中即可:

MessageCell *cell = [tableView dequeueReusableCellWithIdentifier: @"messageCell"];

if (condition cell should be checked) //set the cells checkbox to checked
else //set the cells checkbox to unchecked

或者类似的方法。

重读您的问题时的PS:您真的需要对数组中的单元格进行引用吗?例如,如果您需要用它来创建一个单元格,您不能存储indexPath并使用cellForRowAtIndexPath:吗?

关于ios - 使自定义动态单元格在 UITableView 中不可重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18652815/

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