我正在实现一个颜色选择器表格视图,用户可以在其中选择10种颜色(取决于产品)。用户也可以选择其他选项(如硬盘容量,...)。当选择单元格时,UITableViewCell子视图消失
所有颜色选项都在它们自己的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,我只是将单元格的附件更改为复选框或没有任何内容,并取消选择当前的indexPath)。
什么upadteCell一回事呢? – Idan
updateCell会进行一些微小的调整,如设置选中标记或不选中,根据可用性选择文本颜色,但不会涉及单元格本身或colorThumb。 – Cyrille
接受的答案不提供解决方案,请参阅我的答案以下解答 –