我已经创建了一个自定义UITableViewCell。表视图很好地显示了数据。我陷入的是当用户触摸tableview的单元格时,然后我想显示单元格的背景颜色,而不是默认的[蓝色]值,以突出显示单元格的选择。 我使用这段代码,但什么都没有发生:

cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];

当前回答

Swift 3:对我来说,当你把它放在cellForRowAtIndexPath:方法中时,它就工作了

let view = UIView()
view.backgroundColor = UIColor.red
cell.selectedBackgroundView = view

其他回答

Swift 3:对我来说,当你把它放在cellForRowAtIndexPath:方法中时,它就工作了

let view = UIView()
view.backgroundColor = UIColor.red
cell.selectedBackgroundView = view

以下是我在iOS 8中使用的。

我必须将选择样式设置为UITableViewCellSelectionStyleDefault,以便自定义背景色能够工作。如果有其他样式,自定义背景色将被忽略。在行为上似乎有一个变化,因为以前的答案需要将style设置为none。

单元格的完整代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // This is how you change the background color
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor redColor];
    [cell setSelectedBackgroundView:bgColorView];        
    return cell;
}

再一个提示基督教的方式显示圆角背景分组表。

如果我为单元格使用cornerRadius = 10,它会显示四个角的圆角选择背景。这和表格视图的默认UI不一样。

所以,我想用简单的方法来解决它。 正如你可以从下面的代码中看到的,检查单元格的位置(顶部,底部,中间或顶部底部),并添加更多的子层来隐藏顶部角或底部角。这只是显示了与默认表视图的选择背景完全相同的外观。

我用iPad splitterview测试了这段代码。你可以根据需要改变patchLayer的帧位置。

请让我知道是否有更简单的方法来达到同样的效果。

if (tableView.style == UITableViewStyleGrouped) 
{
    if (indexPath.row == 0) 
    {
        cellPosition = CellGroupPositionAtTop;
    }    
    else 
    {
        cellPosition = CellGroupPositionAtMiddle;
    }

    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
    if (indexPath.row == numberOfRows - 1) 
    {
        if (cellPosition == CellGroupPositionAtTop) 
        {
            cellPosition = CellGroupPositionAtTopAndBottom;
        } 
        else 
        {
            cellPosition = CellGroupPositionAtBottom;
        }
    }

    if (cellPosition != CellGroupPositionAtMiddle) 
    {
        bgColorView.layer.cornerRadius = 10;
        CALayer *patchLayer;
        if (cellPosition == CellGroupPositionAtTop) 
        {
            patchLayer = [CALayer layer];
            patchLayer.frame = CGRectMake(0, 10, 302, 35);
            patchLayer.backgroundColor = YOUR_BACKGROUND_COLOR;
            [bgColorView.layer addSublayer:patchLayer];
        } 
        else if (cellPosition == CellGroupPositionAtBottom) 
        {
            patchLayer = [CALayer layer];
            patchLayer.frame = CGRectMake(0, 0, 302, 35);
            patchLayer.backgroundColor = YOUR_BACKGROUND_COLOR;
            [bgColorView.layer addSublayer:patchLayer];
        }
    }
}

对于Swift 3.0:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = super.tableView(tableView, cellForRowAt: indexPath)

    cell.contentView.backgroundColor = UIColor.red
}
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];

确保你已经使用了上面的行来使用选择效果