This question already has answers here:
Changing background color of selected cell?

(30个答案)


5年前关闭。




我正在制作一个应用程序,用UITableView显示数据。我被困住了。我想更改所选单元格的颜色。这个怎么做?

下面是我在-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];
}

最佳答案

您的cellForRowAtIndexPath中有两个选项只能选择Blue,另一个是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.. //

}

或在另一个选择中,否2 调用此方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 UIView * changeselectionColor = [[UIView alloc] init];
 changeselectionColor.backgroundColor = [UIColor GreenColor];
 cell.selectedBackgroundView = changeselectionColor;
}

10-06 13:23