gpt4 book ai didi

iphone - UITableViewCell 重用 : cell displaying incorrectly - not being reused as expected

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

我创建了一个多选列表,允许选择多种成分。

在我的表格 View 中,可以根据成分的列表属性启用或禁用单元格。如果设置了 Ingredient 的列表属性,则单元格将被禁用。

但是,当一个单元格被重新使用时,它并没有像我预期的那样显示。下面的图片比我更有效地解释了这个问题。

(不应启用的成分有:蛋糕糖衣、炼乳和谷粉。)

  1. 第一张图片显示了三种成分被禁用并按预期进行了注释。

  2. 但是,在第二张图片中,向下滚动会显示某些成分显示为禁用(但您可以选择它们,并且它们具有完整的交互作用)。

  3. 第三张图片显示了向上滚动到顶部后的列表。一些成分已灰显,请注意 Jade 米粉是如何显示为已启用的,即使您无法交互/选择它也是如此。

这个问题与单元重用有关。似乎细胞在重复使用时没有得到“重置”,因此保留了它的一些“旧”外观。

下面是来自 cellForRowAtIndexPath: 的代码,因为我确定这就是问题所在(尽管我看不出有什么问题)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Fetches the corresponding Ingredient from the ingredientArray
Ingredient *ingredient = [self.ingredientArray objectAtIndex:indexPath.row];

if ([ingredient.list isEqualToString:@"Lardr"])
{
cell.userInteractionEnabled = NO;
cell.detailTextLabel.text = @" Already in your Lardr";
}
else if ([ingredient.list isEqualToString:@"Shopping List"])
{
cell.userInteractionEnabled = NO;
cell.detailTextLabel.text = @" Already on your Shopping List";
}
else
{
cell.userInteractionEnabled = YES;
cell.detailTextLabel.text = @"";
}

// Add a checkmark accessory to the cell if the ingredient is on the selectedIngredients array
if ([self.selectedIngredients containsObject:ingredient])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;

cell.textLabel.text = ingredient.name;

return cell;
}

我已经无计可施了,我已经阅读了所有相关的 SO 问题,但无济于事。有什么问题吗?!


我的逻辑是,对于每个单元格,设置了 textLabel、detailTextLabel、userInteractionEnabled 和 accessoryType 属性,无论通过 if 语句的哪个执行路径,所以我不明白为什么,即使在重用之后,单元格也没有正确显示。


编辑: 为了找出问题的根源,我尝试通过在获取相应成分的行上方添加以下内容,将单元格“重置”回默认值: 但无济于事。

cell.userInteractionEnabled = YES;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
cell.accessoryType = UITableViewAccessoryNone;

然而,有趣且完全不合逻辑的是——当我将以下行 cell.textLabel.text = ingredient.name 移动到 Ingredient *ingredient = [ self.ingredientArray objectAtIndex:indexPath.row];,绝对没有样式应用于任何单元格,即使在下面进一步设置了 userInteraction(并且相关单元格按预期被禁用)。

我在想,设置单元格属性的顺序重要吗?我知道不应该,但外观会根据上述情况发生变化。


更新:我已经解决了这个问题;请参阅下面的答案。

最佳答案

willDisplayCell 中明确设置单元格的颜色

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

Ingredient *ingredient = [self.ingredientArray objectAtIndex:indexPath.row];

if ([ingredient.list isEqualToString:@"Lardr"])
{
cell.textLabel.textColor = [UIColor lightGrayColor];
}
else if ([ingredient.list isEqualToString:@"Shopping List"])
{
cell.textLabel.textColor = [UIColor lightGrayColor];
}
else
{
cell.textLabel.textColor = [UIColor blackColor];
}

}

关于iphone - UITableViewCell 重用 : cell displaying incorrectly - not being reused as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777890/

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