Search code examples
iphoneobjective-cipaduitableviewhighlighting

Highlight the borders of the selected cell in UITableView


Is there any possibility to highlighting the borders of the selected cell in a UITableViewController in Objective-c?


Solution

  • You can try to make an custom UIView/UIImageView for the selection with setSelectedBackgroundView:

    Here is a example code I use for custom gradient in a custom tableviewcell:

    UIView *selctionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease];
    
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = selctionView.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor], (id)[[UIColor redColor] CGColor], nil];
    
    [selctionView.layer insertSublayer:gradient atIndex:0];
    
    [self setSelectedBackgroundView:selctionView];
    

    EDIT:

    I found out that you also can use the methods:

    [test.layer setBorderColor: [[UIColor redColor] CGColor]];
    [test.layer setBorderWidth: 1.0]; 
    

    for the layer.

    be sure to import QuartzCore.h

    else for the whole tableview:

    [tableViewController.tableView setSeparatorColor:[UIColor redColor]];