我正在实现一个颜色选择表视图,用户可以在其中选择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 …Run Code Online (Sandbox Code Playgroud) 我在界面构建器中有一个简单的tableViewCell构建.它包含一个包含图像的UIView.现在,当我选择单元格时,会显示默认的蓝色选择背景,但我的UIView的backgroundColor消失了.
我的UITableViewCell的实现文件没有做任何特别的事情.它只是init和&返回self,我在setSelected中所做的就是调用super.
如何在选择tableView时显示我的UIView backgroundColor?
我想在UITableViewCell点击它时使用默认突出显示.但是,我不希望自定义子视图(及其子视图)接收消息以更新其突出显示的状态,从而破坏backgroundColor属性.
编辑
"子视图"我指的是任何子UIView类,而不仅仅是UITableViewCells.
也许这种假设的情况会更好地表达我正在寻找的东西:我有一个UITableViewCell.叫它c.然后我添加一个UIView(称之为v)作为c的子视图.当我点击c时,我想要c突出显示(标准蓝色背景,白色字体颜色),但我不希望v突出显示.我该如何实现这一目标?
这是我的UITableViewCell的.m文件.
#import "ContentCardTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation ContentCardTableViewCell
- (void)awakeFromNib {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[self setBackgroundColor:[UIColor clearColor]];
// Recover backgroundColor of subviews.
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
[self setBackgroundColor:[UIColor clearColor]];
// Recover backgroundColor of subviews.
}
}
@end
Run Code Online (Sandbox Code Playgroud)
但是这个UITableViewCell中的一个视图在选择时消失了.我曾尝试这个和这个,还有更多,但没有任何帮助.有什么我想念的吗?