可点击的复选标记附件如何?

时间:2014-04-06 09:27:41

标签: objective-c uitableview accessory checkmark

我在TableViewController中有简单的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(debug == 1){
        NSLog(@"Running %@ '%@'",self.class, NSStringFromSelector(_cmd));
    }

    static NSString *cellIndetifier = @"Student Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier
                                                            forIndexPath:indexPath];

    Student *student = [self.frc objectAtIndexPath:indexPath];
    NSMutableString *title = [NSMutableString stringWithFormat:@"%@", student.name];

    [title replaceOccurrencesOfString:@"(null)"
                           withString:@""
                              options:0
                                range:NSMakeRange(0, [title length])];

    cell.textLabel.text = title;

    if (cell == nil) {
        cell.textLabel.textColor = [UIColor whiteColor];
    }

    if ([self.checkedIndexPaths containsObject:indexPath])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    if(debug == 1){
        NSLog(@"Running %@ '%@'",self.class, NSStringFromSelector(_cmd));
    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    self.checkedIndexPaths = [self initializationNSSet:self.checkedIndexPaths];

    if ([self.checkedIndexPaths containsObject:indexPath])
    {
        [self.checkedIndexPaths removeObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        [self.checkedIndexPaths addObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

代码工作正常,当我使用DetailButton和DetailDisclosureButton而不是Checkmark和None时,但是当我使用checkmark时没有表格视图的行在两种情况下都不可点击(方法ableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath是不叫)。如何更改Checkmark和None(也许可以点击整行)。我需要带有复选标记的多行,并且当我再次点击它时也会删除选中的行。

1 个答案:

答案 0 :(得分:1)

如果您没有公开按钮,则永远不会调用tableView:accessoryButtonTappedForRowWithIndexPath:

相反,您需要使用tableView:didSelectRowAtIndexPath:

除了:

此代码永远无法正常工作,就好像cell nil您无法在其上调用方法一样:

if (cell == nil) {
    cell.textLabel.textColor = [UIColor whiteColor];
}