我正在實現一個顏色選擇器表格視圖,用戶可以在其中選擇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
接受的答案不提供解決方案,請參閱我的答案以下解答 –