简体   繁体   中英

iOS / iPhone - setting the color of an arbitrary row

I'm trying to set the color of the selected row in a UITableView, using this code:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {   
        cell.backgroundColor = [UIColor redColor];
    }
}

The problem is, it's setting the color on all the rows. Furthermore, when I do select a specific row, it highlights that row in blue. How do I get it to just highlight the selected row, in red?

The easiest way to me is to subclass UITableViewCell and overwrite the setSelected: method (leave everything else the same if you need to)

some sample code from an old project:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    UIView *bg = [[UIView alloc] initWithFrame:self.frame];
    bg.backgroundColor = [UIColor colorWithRed:.916 green:.9648 blue:.9844 alpha:1.0];
    self.selectedBackgroundView = bg;
    [bg release];
    // Configure the view for the selected state
}

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