1

I am making an app in which I am showing my data in a UITableView. I am stuck. I want to change the colour of the selected cell. How to do this?

Below is my code in -didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"the messageid==%@",[[self.inboxmessagesarray objectAtIndex:indexPath.row ] objectForKey:@"messageId"]);
    manage.messageid=[[self.inboxmessagesarray objectAtIndex:indexPath.row ] objectForKey:@"messageId"];  // here i pass the value to singleton class
    [self performSegueWithIdentifier:@"segueIdentifier" sender:tableView];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Mishal Awan
  • 724
  • 2
  • 11
  • 25

3 Answers3

3

Update your code as mentioned below:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"the messageid==%@",[[self.inboxmessagesarray objectAtIndex:indexPath.row ] objectForKey:@"messageId"]);

    manage.messageid = [[self.inboxmessagesarray objectAtIndex:indexPath.row ] objectForKey:@"messageId"];  // here i pass the value to singleton class

    [self performSegueWithIdentifier:@"segueIdentifier" sender:tableView];

    // Add this line to set selected default gray style
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
}

Use selectionStyle property to get cell highlighed. For more details refer UITableViewCellSelectionStyle

Mrunal
  • 13,982
  • 6
  • 52
  • 96
1

May be this will be useful to u...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   UIView *selectedRowColor = [[UIView alloc] init];
   selectedRowColor.backgroundColor = [UIColor redColor];
   cell.selectedBackgroundView = selectedRowColor;
}
Ramesh
  • 102
  • 11
0

in your cellForRowAtIndexPath has two options for selection only Blue and another one is Gray

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{

   /// here your cell identifier name and details
 UIView *changeselectionColor = [[UIView alloc] init];
changeselectionColor.backgroundColor = [UIColor colorWithRed:(245.0/255.0) green:(245.0/255.0) blue:(245.0/255.0) alpha:1];   // change the RGB as you like
cell.selectedBackgroundView = changeselectionColor;

//...... here add your cell details.. //

}

or in another choice no 2 call this method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 UIView * changeselectionColor = [[UIView alloc] init];
 changeselectionColor.backgroundColor = [UIColor GreenColor];
 cell.selectedBackgroundView = changeselectionColor;
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • @MishalAwan- mark this as your answer if it worked for you so that it helps other stack users too – z22 Dec 15 '14 at 06:08