选择带有CheckmarkAccessory和单元重用标识符的UITableViewCells

时间:2014-01-14 06:45:13

标签: ios objective-c uitableview

好吧我正在使用单元格重用标识符在我的表视图上生成UITableViewCells。这个UITableView会显示您的所有联系人名字和姓氏,首先抓住他们ABAddressBook然后使用cellForRowAtIndexPath:中的数据,如下所示:

- (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)?@"":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)?@"":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个单元格,或者不管它是什么。

明确说明:我需要跟踪检查哪些单元格,我需要使用最少的代码执行此操作。我可能错了,但我相信我的情况,我必须使用单元格重用标识符,单元格必须“单选”且不可选。我该怎么做?

1 个答案:

答案 0 :(得分: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;
}

这样,您可以选择多个单元格并取消选择之前选择的任何单元格。 复选标记将根据其早期状态启用或禁用。

希望我对你想做的事情有正确的理解。

相关问题