仅更改自定义 UITableViewCell 选定背景颜色的一部分 [英] Changing Only Part of a Custom UITableViewCell Selected Background Color

查看:23
本文介绍了仅更改自定义 UITableViewCell 选定背景颜色的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom cell that I am controlling the color of when it is selected, ie the example code below:

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

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

This works, however I would only like to have part of the cell change colors when selected. My custom cell is broken down into many different subviews, and I have it sectioned out where I would be able to define the specific view that I would like to change colors for.

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

How can I control the selectedBackgroundView, or use a different method, to have the background color change encompass a single subview in my cell?

推荐答案

Ya you are in the rite way, by subclassing the UITableView cell听到的是示例代码,您可能会找到问题的答案:)

Ya you are in the rite way,by subclassing the UITableView cell hear is the sample code that you may find the answer for your question :)

 //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"

hope helps u :) note: i am using "without ARC"

这篇关于仅更改自定义 UITableViewCell 选定背景颜色的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆