
在我的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]; }} 总结 以上是内存溢出为你收集整理的iphone – UITableViewCell重用:单元格显示不正确 – 未按预期重复使用全部内容,希望文章能够帮你解决iphone – UITableViewCell重用:单元格显示不正确 – 未按预期重复使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)