如何为 UITableView 中的选定单元格制作透明背景

Posted

技术标签:

【中文标题】如何为 UITableView 中的选定单元格制作透明背景【英文标题】:How can I make a transparent background for selected cells in UITableView 【发布时间】:2013-04-08 20:55:04 【问题描述】:

如何使单元格透明。我只想用我已经完成的复选标记显示选定的单元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
        cell.accessoryType = UITableViewCellAccessoryNone;
     
    else 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    

当我第一次创建单元格时,我会执行下一行代码以消除蓝色背景

cell.selectionStyle = UITableViewCellSelectionStyleNone;

但我有一个奇怪的问题,需要单击 2 次才能添加和删除复选框。也许这不是正确的做法?

【问题讨论】:

【参考方案1】:

您可以在此处阅读有关使UITableViewCells 透明化的信息:How to create a UITableViewCell with a transparent background

至于你的第二个问题,看来这可能是你真正想要的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)path 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    cell.accessoryType = UITableViewCellAccessoryNone;

【讨论】:

我添加的透明背景代码很好,但你给我看的第二部分是我需要的

以上是关于如何为 UITableView 中的选定单元格制作透明背景的主要内容,如果未能解决你的问题,请参考以下文章