【问题标题】:Changing Only Part of a Custom UITableViewCell Selected Background Color仅更改自定义 UITableViewCell 选定背景颜色的一部分
【发布时间】:2013-08-08 14:20:21
【问题描述】:

我有一个自定义单元格,我控制它被选中时的颜色,即下面的示例代码:

UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[selectedBackground setBackgroundColor:[UIColor selectedCellColor]];
self.selectedBackgroundView = selectedBackground;

这可行,但是我只想让部分单元格在选择时更改颜色。我的自定义单元格被分解为许多不同的子视图,我将其划分为可以定义我想要更改颜色的特定视图的位置。

如何控制 selectedBackgroundView 或使用不同的方法使背景颜色更改包含单元格中的单个子视图?

【问题讨论】:

    标签: uitableview


    【解决方案1】:

    是的,通过子类化 UITableView 单元格,你是在仪式上 听到是您可能会找到问题答案的示例代码:)

     //in subclassed cell class
    
     - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
      {
       self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
       if (self) {
        // Initialization code
    
        self.frame = CGRectMake(0, 0, 334, 250);
        UILabel *aLabel1= [[UILabel alloc]init];
        UILabel *aLabel2 = [[UILabel alloc]init];
    
    
        self.label1 = aLabel1;
        self.label2 = aLabel2;
    
    
        self.label1.text = @"Happy";
        self.label2.text = @"coding";
    
    
    
    
        UIImageView *bg1 = [[UIImageView alloc]init];
        bg1.tag = 100;
    
        UIImageView *bg2 = [[UIImageView alloc]init];
        bg2.tag = 200;
    
        [self addSubview:bg1]; // you must add your background views first
        [self addSubview:bg2];
    
        [self addSubview:label1];//then other views
        [self addSubview:label2];
    
        [aLabel1 release];
        [aLabel2 release];
    
        [bg1 release];
        [bg2 release];
    
    }
    return self;
    
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
       // Configure the view for the selected state
    
        // hear only you can manage your background views, simply i am adding 2 imageviews by setting different colors 
    [super setSelected:selected animated:animated];
    self.backgroundColor = [UIColor greenColor];
    if(selected)
    {
        self.label1.backgroundColor = [UIColor redColor];
        self.label2.backgroundColor = [UIColor brownColor];
        UIImageView *bg1 = (UIImageView *)[self viewWithTag:100];
        bg1.frame = CGRectMake(0, 0,334/2, 250);
        bg1.backgroundColor = [UIColor yellowColor];
    
    
    }
    else
    {
        self.label1.backgroundColor = [UIColor brownColor];
        self.label2.backgroundColor = [UIColor redColor];
        UIImageView *bg2 =(UIImageView *) [self viewWithTag:200];
        bg2.frame =  CGRectMake(35, 0, 334/2, 250);
        bg2.backgroundColor = [UIColor lightGrayColor];
     }
    
    }
    
       -(void)layoutSubviews
      {
    
        //i am setting the frame for each views that i hav added
       [super layoutSubviews];
       self.label1.frame = CGRectMake(10, 10, 60, 35);
        self.label2.frame = CGRectMake(65, 10, 60, 35);
    
     }
    


    希望能帮助你:) 注意:我使用的是“无 ARC”

    【讨论】:

    • 所以我应该更具体地说明我使用的是什么以及我的确切问题。我已经实现了像你这样的解决方案,但后来我不得不添加“cell.selectionStyle = UITableViewCellSelectionStyleNone;”得到我想要的结果。我正在使用 ARC 并使用代码中的约束创建我的单元 subivews,所以我的答案不同,但具有相同的基本原则。感谢您抽出宝贵时间提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多