2011-02-03 130 views
3

我有一个基本的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

回答

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 
} 
+1

感谢您的答复!这工作几乎完美!你知道是否可以删除出现在选定单元格上的绿松石高光? – 2011-02-03 20:40:34