Question

I have simple code in my 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;
    }
}

The code works fine, when I use DetailButton and DetailDisclosureButton instead of Checkmark and None, but when I use checkmark and none the row of table view isn't clickable in neither case (the method ableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath is not called). How can I do to get changed Checkmark and None (maybe do clickable whole row). I need multiple rows with checkmark and also remove the checked row, when I click on it again.

Was it helpful?

Solution

If you don't have a disclosure button then tableView:accessoryButtonTappedForRowWithIndexPath: will never be called.

Instead, you would need to use tableView:didSelectRowAtIndexPath:.

Aside:

This code is never going to work usefully as if cell is nil you can't call a method on it:

if (cell == nil) {
    cell.textLabel.textColor = [UIColor whiteColor];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top