Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅更改自定义UITableViewCell选定背景色的一部分_Uitableview - Fatal编程技术网

仅更改自定义UITableViewCell选定背景色的一部分

仅更改自定义UITableViewCell选定背景色的一部分,uitableview,Uitableview,我有一个自定义单元格,当它被选中时,我控制它的颜色,即下面的示例代码: UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)]; [selectedBackground setBackgroundColor:[UIColor selectedCellColor]]; self.selectedBackgroundView = selectedBackground; 这是可行的,

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

UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[selectedBackground setBackgroundColor:[UIColor selectedCellColor]];
self.selectedBackgroundView = selectedBackground;
这是可行的,但我只想让部分单元格在选中时更改颜色。我的自定义单元格被分解为许多不同的子视图,我将它剖切出来,在那里我可以定义我想要更改颜色的特定视图


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

Ya通过对UITableView单元格进行子类化,您就是在用rite的方式 hear是您可以找到问题答案的示例代码:)


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

Ya,通过对UITableView单元格进行子类化,您就是在用rite的方式 hear是您可以找到问题答案的示例代码:)


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

,因此我应该更具体地说明我使用的是什么以及我的确切问题。我已经实现了一个类似于您的解决方案,但后来我不得不添加“cell.selectionStyle=UITableViewCellSelectionStyleNone;”以获得我想要的结果。我使用ARC并使用代码中的约束创建我的单元格子视图,所以我的答案不同,但有相同的基本原则。谢谢你花时间帮忙!所以我应该更具体地说明我在使用什么以及我的确切问题。我已经实现了一个类似于您的解决方案,但后来我不得不添加“cell.selectionStyle=UITableViewCellSelectionStyleNone;”以获得我想要的结果。我使用ARC并使用代码中的约束创建我的单元格子视图,所以我的答案不同,但有相同的基本原则。谢谢你花时间帮忙!
 //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);

 }