2011-01-05 48 views

回答

4

實現您的表視圖代表tableView:didSelectRowAtIndexPath:,搶細胞這是使用[tableView cellForRowAtIndexPath:indexPath]選擇的,並將其附件設置爲UITableViewCellAccessoryCheckmark

如果你想實現類似於在設置應用程序的多值表意見的東西,我做的是使用一個屬性來跟蹤那些已經選擇的年代:

@property (nonatomic, retain) NSIndexPath *selectedIndexPath; 

在相同的委託方法我在選擇刪除單元格中的複選標記,複選標記添加到被竊聽的細胞和更新屬性與該小區的指標:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath]; 
    [oldCell setAccessory:UITableViewCellAccessoryNone]; 

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    [newCell setAccessory:UITableViewCellAccessoryCheckmark]; 

    [self setSelectedIndexPath:indexPath]; 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

要多走一步,你可以找到一種方法在應用中保留索引路徑(可能使用NSUserDefaults,並在tableView:cellForRowAtIndexPath:將複選標記應用於與該索引路徑對應的任何單元格,以便您的複選標記顯示在視圖加載上。

+0

問題是,這種選擇不止一個,但我想提出檢查只有一排 – Ali 2011-01-05 06:54:23

+0

很不錯的答案,謝謝 – Ali 2011-01-05 07:13:31