简体   繁体   中英

selected row background color not changing in ios

I don't know the reason why the selected row color is not changing in tableview.

I created custom cell and applied gradient color to the background of cell and also i wrote code to change the bgcolor using selected background. The problem is if i didn't use gradient color it is working well but if use gradient color it is not changing the bgcolor.

What is the exact problem?? any solution for this??

  -(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];

}

Try to avoid using this code in your -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method

    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];

With every call to this method UIView v will be created and applied to your cell, thus resulting in a huge memory overhead when you scroll tableview.

As for the question, because you are subclassing UITableViewCell, then implement the code for regular background in either -(void)drawRect:(CGRect)rect or in init. And then create UIView that will be your gradient for selected background and apply to your cell subclass:

- (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;
}

Hope this is helpful, cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM