单击表格单元格时的阴影背景

时间:2012-06-30 20:22:50

标签: iphone objective-c ios xcode

我用自定义单元格制作了一张桌子。每个单元格都是一个文本字段。我希望当我点击随机单元格时,其他未聚焦的单元格上会出现一个阴影层(透明的bockground),如下图所示。

http://i47.tinypic.com/nwd45e.png

1 个答案:

答案 0 :(得分:1)

如果你在storyboard / xib中将tableViewCell的背景设置为默认颜色(基本上很清楚,以便让tableView颜色成为你想要的颜色),那么你可以执行以下操作。

// assume that the background color for the tableView in the storyboard is lightOrange,
// as seen in http://i47.tinypic.com/nwd45e.png

- (UITableViewCell*)tableView:(UITableView*)tableView
        cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    ... the normal code for getting your cell, probably involving DequeueReusableCell ...

    // yes, the following gets called for every row even when selection doesn't change,
    // but it is a very small hit.  could be replaced by overriding reloadData for the
    // tableView, but that isn't always desirable.
    if (tableView.indexPathForSelectedRow)
        tableView.backgroundColor = [UIColor grayColor];
    else
        tableView.backgroundColor = [UIColor lightOrangeColor];

    return cell;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    ... whatever else you would do for a selected row, perhaps a segue or something ...
    ... (and if part of this is to de-select a row that is already selected, take that ...
    ... into account as a conditional for the following line of code.

    tableView.backgroundColor = [UIColor grayColor];
}