2011-05-28 79 views
5

在我的UITableView中,當它進入編輯模式時,我只想選擇一些選定的單元格。我知道UITableView班有財產allowsSelectionDuringEditing,但這適用於整個UITableView。我沒有看到任何相關的委託方法在每個單元的基礎上進行設置。UITableView編輯時的單元格選擇模式

我能想出的最佳解決方案是將allowsSelectionDuringEditing設置爲YES。然後,在didSelectRowAtIndexPath中,如果表視圖正在編輯,則過濾掉任何不需要的選擇。另外,在cellForRowAtIndexPath中,將這些單元格selectionStyle更改爲無。

問題是進入編輯模式不會重新加載UITableViewCells,所以他們的selectionStyle直到他們滾動離屏時纔會更改。所以,在setEditing中,我還必須迭代可見單元格並設置它們的selectionStyle

這有效,但我只是想知道是否有更好/更優雅的解決方案來解決這個問題。我的代碼的基本輪廓附加。任何建議非常感謝!謝謝。

- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 

    if (self.editing && ![self _isUtilityRow:indexPath]) return; 
    // Otherwise, do the normal thing... 
} 

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

    // UITableViewCell* cell = ... 

    if (self.editing && ![self _isUtilityRow:indexPath]) 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    else 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    } 

    return cell; 
} 

- (void) setEditing:(BOOL)editing animated:(BOOL)animated { 

    [super setEditing:editing animated:animated]; 

    if (editing) 
    { 
     for (UITableViewCell* cell in [self.tableView visibleCells]) 
     { 
      if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]]) 
      { 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      } 
     } 
    } 
    else 
    { 
     for (UITableViewCell* cell in [self.tableView visibleCells]) 
     { 
      if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]]) 
      { 
       cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      } 
     } 
    } 
} 
+0

您可以在進入編輯模式時重新加載表格,或者在選擇該表格後立即取消選擇該行,而不是將選擇樣式設置爲無... – AMayes 2013-01-10 21:29:11

回答

0

我不知道你是如何應用的工作原理,但或許你可以嘗試使用下面的某處你的數據源定義:

// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable. 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

進入編輯模式時,使用功能過濾第一個選擇級別,然後進入第二個選擇級別

+0

這應該工作。 – CW0007007 2013-10-02 13:43:32

相關問題