views:

41

answers:

3

how to set custom color of table row when it is clicked...

i mean i don't want that blue color which appear by Default....

+1  A: 

you can set the tableView cell property

cell.selectionStyle = UITableViewCellSelectionStyleGray; 
Gyani
only blue and gray colors?.....
Ranjeet Sajwan
+1 Gyani ji reply Kijiye
Ranjeet Sajwan
yes by that will get blue and gray colors
Gyani
+2  A: 

To get whatever color you want, you have to replace the selectedBackgroundView with your own UIView.

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellID";
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIView *selectedBackground = [[[UIView alloc] init] autorelease];
        selectedBackground.backgroundColor = [UIColor magentaColor];
        cell.selectedBackgroundView = selectedBackground;
    }
    // configure cell
    return cell;
}
fluchtpunkt
+1 thanks...i tried it and was success but the frame is not roundRect....
Ranjeet Sajwan
yes, this will not work in grouped style. It's much harder to achieve the same thing with a grouped style. One way is to subclass UIView, like done in http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/
fluchtpunkt
+1  A: 

Make your own subclass of UITableViewCell. Overwrite the method -(void)setSelected:(BOOL)selected

Something like this:

- (void)setSelected:(BOOL)selected {
       [super setSelected:selected];
       if (selected) {
             self.backgroundColor = [UIColor greenColor];
       } else {
             self.backgroundColor = [UIColor whiteColor];
       }
}
burki
+1 : ok but the problem is how to use it with my class which has already the tableview and which is subclass of UIViewController...actually a never used subclass of UITableViewCell...so please give idea accordingly....
Ranjeet Sajwan