多选表格视图单元格,无选择样式

时间:2011-02-03 19:46:48

标签: iphone cocoa-touch uitableview

我有一个基本的UITableView,我想在没有选择样式的情况下启用 Mail.app 样式复选标记。我有以下代码段:

#define UITableViewCellEditingStyleMultiSelect (3)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleMultiSelect;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

但是,这将永远不会在选择上显示复选标记(尽管显示空心圆圈)。关于如何修复它的任何想法?我知道它使用了未记录的功能,但我真的想添加对复选标记的支持。我的实际示例使用了非常自定义的UITableViewCell,我无法启用选择样式!

sample table view

1 个答案:

答案 0 :(得分:6)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    if (self.tableView.isEditing) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleMultiSelect;
}

-(IBAction) switchEditing {
    [self.tableView setEditing:![self.tableView isEditing]];
    [self.tableView reloadData]; // force reload to reset selection style
}
相关问题