选中的行背景颜色在ios中没有变化

时间:2013-08-12 13:10:42

标签: ios objective-c uitableview custom-cell

我不知道所选行颜色在tableview中没有变化的原因。

我创建了自定义单元格并将渐变颜色应用到单元格的背景中,我还编写了代码以使用所选背景更改bgcolor。问题是,如果我没有使用渐变色,它运作良好,但如果使用渐变色,它不会改变bgcolor。

究竟是什么问题?任何解决方案??

  -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellidentifier=@"ViewProfileCell";

    MyHomeViewCell *cell= [[MyHomeViewCell alloc] init];

    cell=(MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if(!cell)
    {
NSArray *nibofMyHomeCell=[[NSBundle mainBundle]loadNibNamed:@"MyHomeViewCell" owner:self options:Nil];
        cell=[nibofMyHomeCell objectAtIndex:0];


    }
    UIView *v=[[UIView alloc]init];

    v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor];

    cell.selectedBackgroundView=v;

   CAGradientLayer *ViewGradient=[CAGradientLayer layer];
    ViewGradient.frame=CGRectMake(0, 0, 320, 52);
    ViewGradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:27.0f / 255.0f green:48.0f / 255.0f blue:39.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:26.0f / 255.0f green:47.0f / 255.0f blue:38.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:25.0f / 255.0f green:44.0f / 255.0f blue:37.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:23.0f / 255.0f green:42.0f / 255.0f blue:35.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:41.0f / 255.0f blue:34.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:40.0f / 255.0f blue:33.0f / 255.0f alpha:1.0f] CGColor],
                           nil];

    [cell.layer insertSublayer:ViewGradient atIndex:0];
    cell.layer.shadowColor = [[UIColor whiteColor] CGColor];
    cell.layer.shadowOpacity=20.0f;

    cell.layer.borderWidth=1.0f;

    cell.layer.borderColor=[[UIColor colorWithRed:38.0f / 255.0f green:70.0f / 255.0f blue:58.0f / 255.0f alpha:1.0f] CGColor];

}

1 个答案:

答案 0 :(得分:0)

尽量避免在-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法

中使用此代码
    UIView *v=[[UIView alloc]init];

    v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor];

    cell.selectedBackgroundView=v;

   CAGradientLayer *ViewGradient=[CAGradientLayer layer];
    ViewGradient.frame=CGRectMake(0, 0, 320, 52);
    ViewGradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:27.0f / 255.0f green:48.0f / 255.0f blue:39.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:26.0f / 255.0f green:47.0f / 255.0f blue:38.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:25.0f / 255.0f green:44.0f / 255.0f blue:37.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:23.0f / 255.0f green:42.0f / 255.0f blue:35.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:41.0f / 255.0f blue:34.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:40.0f / 255.0f blue:33.0f / 255.0f alpha:1.0f] CGColor],
                           nil];

    [cell.layer insertSublayer:ViewGradient atIndex:0];
    cell.layer.shadowColor = [[UIColor whiteColor] CGColor];
    cell.layer.shadowOpacity=20.0f;

    cell.layer.borderWidth=1.0f;

    cell.layer.borderColor=[[UIColor colorWithRed:38.0f / 255.0f green:70.0f / 255.0f blue:58.0f / 255.0f alpha:1.0f] CGColor];

每次调用此方法UIView v都会被创建并应用于您的单元格,因此在滚动tableview时会产生巨大的内存开销。

至于问题,因为你是UITableViewCell的子类,然后在-(void)drawRect:(CGRect)rect或init中实现常规后台的代码。然后创建UIView,它将成为所选背景的渐变并应用于您的细胞子类:

- (id)init {
    self = [super init];
    if (self) {
        self.contentView.backgroundColor = [UIColor anyColourYouNeed];
        UIView *v=[[UIView alloc]init];

        v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor];

        cell.selectedBackgroundView=v;
    }
    return self;
}

希望这有用,欢呼!