【发布时间】:2011-10-08 10:09:55
【问题描述】:
我正在实现一个颜色选择器表视图,用户可以在其中选择 10 种颜色(取决于产品)。用户还可以选择其他选项(如硬盘容量,...)。
所有颜色选项都在它们自己的 tableview 部分中。
我想在 textLabel 左侧显示一个小方块,显示实际颜色。
现在我正在添加一个简单的方形 UIView,给它正确的背景颜色,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RMProductAttributesCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:RMProductAttributesCellID] autorelease];
cell.indentationWidth = 44 - 8;
UIView *colorThumb = [[[UIView alloc] initWithFrame:CGRectMake(8, 8, 28, 28)] autorelease];
colorThumb.tag = RMProductAttributesCellColorThumbTag;
colorThumb.hidden = YES;
[cell.contentView addSubview:colorThumb];
}
RMProductAttribute *attr = (RMProductAttribute *)[_product.attributes objectAtIndex:indexPath.section];
RMProductAttributeValue *value = (RMProductAttributeValue *)[attr.values objectAtIndex:indexPath.row];
cell.textLabel.text = value.name;
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *colorThumb = [cell viewWithTag:RMProductAttributesCellColorThumbTag];
colorThumb.hidden = !attr.isColor;
cell.indentationLevel = (attr.isColor ? 1 : 0);
if (attr.isColor) {
colorThumb.layer.cornerRadius = 6.0;
colorThumb.backgroundColor = value.color;
}
[self updateCell:cell atIndexPath:indexPath];
return cell;
}
这显示没有问题。
我唯一的问题是,当我选择“颜色”行时,在淡入蓝色选择动画期间,我的自定义 UIView (colorThumb) 被隐藏。它在选择/取消选择动画结束后再次可见,但这会产生丑陋的伪影。
我应该怎么做才能纠正这个问题?我不是在正确的位置插入子视图吗?
(didSelectRowAtIndexPath没有什么特别的,我只是把cell的附件改成checkbox或者什么都没有,把当前的indexPath取消选中)。
【问题讨论】:
-
upadteCell 是什么?
-
updateCell 做了一些小的调整,比如是否设置复选标记,根据可用性选择文本颜色,......但与单元格本身或 colorThumb 没有真正的变化。
-
接受的答案没有提供解决方案,请参阅下面我的答案以获取解决方案
标签: iphone ios uitableview