- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我创建了一个多选列表,允许选择多种成分。
在我的表格 View 中,可以根据成分的列表属性启用或禁用单元格。如果设置了 Ingredient 的列表属性,则单元格将被禁用。
但是,当一个单元格被重新使用时,它并没有像我预期的那样显示。下面的图片比我更有效地解释了这个问题。
(不应启用的成分有:蛋糕糖衣、炼乳和谷粉。)
第一张图片显示了三种成分被禁用并按预期进行了注释。
但是,在第二张图片中,向下滚动会显示某些成分显示为禁用(但您可以选择它们,并且它们具有完整的交互作用)。
第三张图片显示了向上滚动到顶部后的列表。一些成分已灰显,请注意 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/
我正在使用带有 64 位插件的 64 位操作系统 Windows 7 终极机器 VS2008。 我已经在 32 位和 64 位、调试和发布配置中成功构建了我的项目。64 位调试未启动;它给出了错误:
我遇到了一个我不理解的 java 泛型编译时错误。 我有两种方法: public static final T doStuff(List list, int k, Comparator compar
在下面的类(class)中,我尝试打印工资系统中员工的详细信息。请注意,Porter、Pharmacist 和 Surgeon 均继承自 Employee。 但是,它只是重复打印添加到数组中的第一个员
package chapter5; import java.util.Scanner; public class Exercise5 { public static void main(Str
WITH list_dedup (Company, duplicate_count) AS ( SELECT *, ROW_NUMBER() OVER (
我有一些 base64 编码的数据,即使其中存在填充错误,我也想将其转换回二进制。如果我使用 base64.decodestring(b64_string) 它会引发“不正确的填充”错误。还有其他方法
我正在致力于将大型 Delphi 代码库调整为 64 位。在许多情况下,有些行将指针转换为 32 位值或从 32 位值转换为类似于以下内容的行: var p1,p2 : pointer; begi
我正在尝试在 rtaudio 中生成一个简单的正弦波,以验证我了解发生了什么。但是,结果是错误的。 我有一个全局float timec ,以及使用 openStream 调用的回调它应该用样本填充缓冲
将我们的 Jenkins 主安装更新到最新的 LTS 版本 2.46.3 后,其从属设备之一(Windows 7 计算机,32 位)无法与主设备连接。 我们收到的错误是: java -jar slav
实现ROCR曲线,kNN,K进行10倍交叉验证。 我正在使用电离层数据集。 这是属性信息,供您引用: -如上所述,所有34个都是连续的 -根据定义,第35个属性为“好”或“坏” 以上总结。这是一个二进
我正在阅读有关“Servlet 3.0 中的异步处理支持”的专家(?)教程(http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.h
我目前正在为我即将开展的项目制作自己的关卡创建器(图 block map )。一切都很顺利,只是当我添加放大和缩小选项时遇到了问题。我有一个类正在处理当前的所有输入和渲染,因为我刚刚开始。 Level
我在 Eclipse mars 2.0 中使用 Mockito(1.10.19) 进行 Java EE 测试来测试离线存储库。此类依赖于 InitialData 类来检索信息。 我的第一个任务是将地址
我正在尝试实现“算法简介”一书中所述的合并排序算法。尽管实现是按照书中指定的,但输出不正确。很有可能出现相差一的错误,但我无法指出它。有什么指点吗? #include #include #defi
我正在尝试确定 Windows 任务栏(系统托盘?)停靠在哪一侧 - 这样我就可以将弹出窗口定位在任务栏的上方/下方/左侧/右侧。 我正在使用 SHAppBarMessage(ABM_QUERYPOS
我正在使用以下公式实现 DCT 变换: 但是结果不正确。对于一些 8 × 8 矩阵, 0 0 0 0 0 0 0 0 210 210 210 210 210 210 21
我正在尝试编写将内存流转换为 png 图像的代码,但在 using(Image img = Image.从流(毫秒))。它没有进一步指定它,所以我不知道为什么会收到错误以及我应该怎么做。 此外,如何将
这个语句工作正常: SELECT * FROM table_name WHERE DATE(date_event) < DATE(NOW() - INTERVAL 90 DAY); 在 DELETE
当我删除图像并尝试保存配置文件时,它显示错误“incorect padding” 我的代码是 模型.py import webcam.admin from webcam import widgets
我正在尝试创建一个函数来计算两个字符串之间的汉明距离。当我调用这个函数时,它应该告诉我两个字符串之间不匹配的字符数。 我的输出不正确。我不断得到随机数结果。下面是我的代码: using namespa
我是一名优秀的程序员,十分优秀!