加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UITableViewCell重用:单元格显示不正确 – 未按预期

发布时间:2020-12-14 19:53:34 所属栏目:百科 来源:网络整理
导读:我创建了一个多选列表,允许选择多种成分. 在我的tableview中,可以根据Ingredient的list属性启用或禁用单元格.如果设置了Ingredient的list属性,则将禁用该单元格. 但是,当重新使用一个单元格时,它不会像我期望的那样显示.下面的图片比我能更有效地解释问题. (
我创建了一个多选列表,允许选择多种成分.

在我的tableview中,可以根据Ingredient的list属性启用或禁用单元格.如果设置了Ingredient的list属性,则将禁用该单元格.

但是,当重新使用一个单元格时,它不会像我期望的那样显示.下面的图片比我能更有效地解释问题.

(不应该启用的成分是:蛋糕糖霜,炼乳和Cournflour.)

>第一张图片显示三种成分已禁用并按预期注释.
>但是,在第二张图片中,向下滚动显示某些成分显示为已禁用(但您可以选择它们,并且它们具有完全交互).
>第三张图像在向上滚动到顶部后显示列表.一些成分已经变灰,并注意到Cornflour显示为已启用,即使您无法进行交互/选择.

问题与细胞重用有关.似乎在重复使用时,单元格没有被“重置”,因此保留了一些“旧”外观.

下面是来自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];
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读