如何在UITableView中为选定的单元格创建透明背景

时间:2013-04-08 20:55:04

标签: ios objective-c xcode uitableview

如何让细胞透明化。我只想用我做过的复选标记显示所选单元格:

- (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 个答案:

答案 0 :(得分:1)

您可以在此处阅读有关制作透明UITableViewCell的信息: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;
}