如何在UITableView中更改所选单元格的颜色?

时间:2014-12-15 05:51:44

标签: ios objective-c uitableview

我正在创建一个应用程序,我在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];
}

3 个答案:

答案 0 :(得分:3)

更新您的代码,如下所述:

-(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;
}

使用selectionStyle属性获取高亮显示的单元格。有关详细信息,请参阅UITableViewCellSelectionStyle

答案 1 :(得分:1)

这可能会对你有用...

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

答案 2 :(得分:0)

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.. //

}

或在另一个选择no 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;
}