Tea*_*Pow 7 iphone xcode uitableview ios
我创建了一个多选列表,允许选择多种成分.
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
我正在努力解决这个问题,并且我已经阅读了所有甚至远程相关的SO问题,但无济于事.有什么问题?!
我的逻辑是,对于每个单元格,设置textLabel,detailTextLabel,userInteractionEnabled和accessoryType属性,无论通过if语句执行哪条路径,所以我看不出为什么,即使重用后,单元格也没有正确显示.
编辑:为了找出问题的根源,我尝试通过在获取相应成分的行上方添加以下内容来"重置"单元格的默认值:但无济于事.
cell.userInteractionEnabled = YES;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
cell.accessoryType = UITableViewAccessoryNone;
Run Code Online (Sandbox Code Playgroud)
然而,有趣且完全不合逻辑的 - 当我将以下行移动cell.textLabel.text = ingredient.name到读取行的正下方时Ingredient *ingredient = [self.ingredientArray objectAtIndex:indexPath.row];,绝对没有样式应用于任何单元格,即使userInteraction被设置在下面(并且相关单元格被按预期禁用) .
我在想,单元属性设置的顺序是否重要?我知道它不应该,但外观根据上述情况而变化.
更新:我已经解决了这个问题; 请参阅下面的答案.
我已经解决了这个问题。问题是我在设置单元格文本之前设置了 userInteractionEnabled (感谢评论中 Carl Veazey 的链接)。
我通过在调整 userInteractionEnabled 属性之前设置单元格的文本解决了该问题。但是,没有对单元格应用样式(即使单元格已禁用)。因此,我必须通过设置单元格的文本颜色来手动设置单元格的样式。下面是调整后的代码和注释。
- (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 relevent ingredient from the ingredientArray
Ingredient *ingredient = [self.ingredientArray objectAtIndex:indexPath.row];
cell.textLabel.text = ingredient.name; // Moved this line before setting userInteractionEnabled
if ([ingredient.list isEqualToString:@"Lardr"])
{
cell.userInteractionEnabled = NO;
cell.detailTextLabel.text = @" Already in your Lardr";
cell.textLabel.textColor = [UIColor grayColor]; // Update the color accordingly
}
else if ([ingredient.list isEqualToString:@"Shopping List"])
{
cell.userInteractionEnabled = NO;
cell.detailTextLabel.text = @" Already on your Shopping List";
cell.textLabel.textColor = [UIColor grayColor];
}
else
{
cell.userInteractionEnabled = YES;
cell.detailTextLabel.text = @"";
cell.textLabel.textColor = [UIColor blackColor];
}
// 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;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
显然,设置单元格属性的顺序应该是无关紧要的,所以我确信这是一个错误。希望这对遇到同样问题的其他人有所帮助。
| 归档时间: |
|
| 查看次数: |
6833 次 |
| 最近记录: |