2011-02-03 186 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