选择单元格时,UITableViewCell子视图消失

Cyr*_*lle 178 iphone uitableview ios

我正在实现一个颜色选择表视图,用户可以在其中选择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;
}
Run Code Online (Sandbox Code Playgroud)

这显示没问题.

我唯一的问题是当我选择一个"颜色"行时,在淡入蓝色选择动画期间,我的自定义UIView(colorThumb)被隐藏.它在选择/取消选择动画结束后再次可见,但这会产生一个丑陋的神器.

我该怎么做才能纠正这个问题?我不能将子视图插入正确的位置吗?

(didSelectRowAtIndexPath没有什么特别之处,我只是将单元格的附件更改为复选框或什么都没有,并取消选择当前的indexPath).

Yat*_*B L 226

UITableViewCell changes the background color of all sub views when cell is selected or highlighted ,You can Solve this problem by overriding Tableview cell's setSelected:animated and setHighlighted:animated and resetting view background color.

In Objective C :

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
   UIColor *color = self.yourView.backgroundColor;        
   [super setSelected:selected animated:animated];

    if (selected){
        self.yourView.backgroundColor = color;
    }
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    UIColor *color = self.yourView.backgroundColor;        
    [super setHighlighted:highlighted animated:animated];

    if (highlighted){
        self.yourView.backgroundColor = color;
    }
}
Run Code Online (Sandbox Code Playgroud)

In Swift 3.1 :

override func setSelected(_ selected: Bool, animated: Bool) {
    let color = yourView.backgroundColor         
    super.setSelected(selected, animated: animated)

    if selected {
        yourView.backgroundColor = color
    }
}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    let color = yourView.backgroundColor
    super.setHighlighted(highlighted, animated: animated)

    if highlighted {
        yourView.backgroundColor = color
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*riy 122

这是因为表格视图单元格会自动更改内容视图中所有视图的背景颜色以突出显示状态.您可以考虑使用子类UIView绘制颜色或使用UIImageView自定义1x1 px拉伸图像.

  • 或者你可以重新设置背景颜色覆盖`setHighlighted:animated:`和`setSelected:animated:` (52认同)

Pav*_*rov 44

找到了一个非常优雅的解决方案,而不是搞乱tableViewCell选择/突出显示方法.您可以创建UIView的子类,忽略将其背景颜色设置为清除颜色.

迅捷3/4:

class NeverClearView: UIView {
    override var backgroundColor: UIColor? {
        didSet {
            if backgroundColor != nil && backgroundColor!.cgColor.alpha == 0 {
                backgroundColor = oldValue
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特2:

class NeverClearView: UIView {
    override var backgroundColor: UIColor? {
        didSet {
            if CGColorGetAlpha(backgroundColor!.CGColor) != 0 {
                backgroundColor = oldValue
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Obj-C版本:

@interface NeverClearView : UIView

@end

@implementation NeverClearView

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    if (CGColorGetAlpha(backgroundColor.CGColor) != 0) {
        [super setBackgroundColor:backgroundColor];
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 我将if更改为CGColorGetAlpha(backgroundColor!.CGColor)== 0之后为我工作,因为它不等于clearColor (3认同)

Aga*_*gat 9

管理问题的另一种方法是使用核心图形渐变填充视图,例如:

CAGradientLayer* gr = [CAGradientLayer layer];
gr.frame = mySubview.frame;
gr.colors = [NSArray arrayWithObjects:
                     (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:.5] CGColor]
                     ,(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:.5] CGColor]
                     , nil];

gr.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:1],nil];

[mySubview.layer insertSublayer:gr atIndex:0];
Run Code Online (Sandbox Code Playgroud)


swi*_*Boy 9

对于Swift 2.2,这是有效的

cell.selectionStyle = UITableViewCellSelectionStyle.None
Run Code Online (Sandbox Code Playgroud)

@ Andriy解释了原因

这是因为表格视图单元格会自动更改内容视图中所有视图的背景颜色以突出显示状态.


Tim*_*eit 8

Yatheesha BL回答启发, 我创建了一个UITableViewCell类别/扩展,允许您打开和关闭这个透明度"功能".

迅速

let cell = <Initialize Cell>
cell.keepSubviewBackground = true  // Turn  transparency "feature" off
cell.keepSubviewBackground = false // Leave transparency "feature" on
Run Code Online (Sandbox Code Playgroud)

Objective-C的

UITableViewCell* cell = <Initialize Cell>
cell.keepSubviewBackground = YES;  // Turn  transparency "feature" off
cell.keepSubviewBackground = NO;   // Leave transparency "feature" on
Run Code Online (Sandbox Code Playgroud)

KeepBackgroundCell与CocoaPods兼容.你可以在GitHub上找到它


小智 7

你可以cell.selectionStyle = UITableViewCellSelectionStyleNone;,然后设置backgroundColor- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath