Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to change the color of table view selection

I need to change the default blue color selection of table view to some custom color. Is there any way to do that. Help me


1 Answers

The best way to do this is like this:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"myCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIView *v = [[[UIView alloc] init] autorelease];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
    }
    // Set up the cell...
    cell.textLabel.text = @"foo";
    return cell;
}

The relevant part for you is the cell.selectedBackgroundView = v; instruction. You can substitute the very basic view 'v' here with any view you like.

like image 148
Zoran Simic Avatar answered Sep 16 '25 06:09

Zoran Simic