1

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

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

4

1 回答 1

1

if you have set your tableViewCell's background in your storyboard/xib to have the default color (which is basically clear to allow the tableView color to be what you want), then you can do something like the following.

// 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];
}
于 2012-06-30T22:09:00.143 回答