2014-01-14 28 views
0

好吧,我正在使用單元重用標識符在我的表視圖上生成UITableViewCells。這UITableView顯示所有聯繫人的姓氏和名字,首先抓住他們通過ABAddressBook,然後使用數據cellForRowAtIndexPath:這樣的:選擇具有CheckmarkAccessory和單元重用標識符的UITableViewCells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"inviteCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // forIndexPath:indexPath]; taken from above 

    if(tableView == self.tableView) 
    { 
     //configure the cells for the contacts tableView 
     #define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)[email protected]"":str 

     id p=[contactsObjects objectAtIndex:indexPath.row]; 
     NSString *fName=(__bridge NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName)); 
     NSString *lName=(__bridge NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName)); 
     cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",CHECK_NULL_STRING(fName),CHECK_NULL_STRING(lName)]; 
    } 
    else 
    { 
     //configure the cells for the search bar controller's tableView. 

     #define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)[email protected]"":str 

     id p=[searchResults objectAtIndex:indexPath.row]; 
     NSString *fName=(__bridge NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName)); 
     NSString *lName=(__bridge NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName)); 
     cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",CHECK_NULL_STRING(fName),CHECK_NULL_STRING(lName)]; 

    } 

    return cell; 

} 

我需要做的是,選擇任意生成的細胞像這樣,也能夠取消選擇任何單元格。在後臺,我將使用與每個姓名關聯的電話號碼來檢查他們是否在我的數據庫中註冊。但至於這UITableView我需要選擇任何表單元格,並且而不是也選擇每10個單元格,或不管它是什麼。

說得很清楚:我需要跟蹤檢查哪些單元格,並且我需要使用最少的代碼來完成此操作。我可能是錯的,但我相信在我的情況下,我必須使用單元重用標識符,並且單元必須「單選」並且不可選。我該怎麼做呢?

+0

請參閱http://stackoverflow.com/a/5973945/1824529 –

回答

1

如果我理解正確..你只需要在tableview上的單一選擇。一旦選擇了新行,您將取消選擇舊行。

你可以做到這一點,像這樣:

的cellForRowAtIndexPath做一些這樣的事。

if(indexPath.row == 0){ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     self.lastSelected = indexPath;//This will keep track of the last selected cell . 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([self.lastSelected compare:indexPath] == NSOrderedSame){ 
     [tableView deselectRowAtIndexPath:indexPath animated:NO];//This is to remove the highlight on cell selection 
     return; 
    } else { 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
     [tableView cellForRowAtIndexPath:self.lastSelected].accessoryType = UITableViewCellAccessoryNone; 
     [self setLastSelected:nil];//Releasing the old retain and resetting 
     self.lastSelected = indexPath; 
     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    } 
} 

希望這有助於

編輯: 好了,現在我明白你的問題。我編輯的回答如下

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) { 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    } else 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

這樣您可以選擇多個單元並取消選擇先前選擇的任何單元。 根據以前的狀態,複選標記將被啓用或禁用。

希望我對你想做什麼有正確的理解。

+0

請檢查我編輯的答案 –

+0

謝謝,除了單元重用標識符外,這正是我想要的。當我選擇一行時,它實際上會在tableView中選擇相應的重用單元格。但是,至今添加和刪除檢查仍然有效 – Chisx

相關問題