SmartTree
SmartTree

Reputation: 1471

Logic for UITableViewCellAccessoryCheckmark

I want to make a typical situation: when user selects any cell, it's accessoryType turns in checkmark. Only one cell's accessoryType can be checkmark. And then I wanna save in NSUserDefaults indexPath.row so my app will be able to know which cell user selected and make some changes in options. So I wrote this wrong code:

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // checkedIndexPath is NSIndexPath

    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.checkedIndexPath = indexPath;

    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInt:self.checkedIndexPath.row]forKey:@"indexpathrow" ];
} 

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Part of code from cellForRowAtIndexPath

    if(indexPath.row == [[[NSUserDefaults standardUserDefaults]objectForKey:@"indexpathrow"]intValue ])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

        return cell;
}

However, this code works badly. When you open the UITableView, there is an already selected cell in the table and when you press another there are two checkmarked cells...How can I improve my code or should I change it whole ? Any suggestions ? Thanks !

Upvotes: 1

Views: 2508

Answers (1)

Denis
Denis

Reputation: 6413

Try this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // checkedIndexPath is NSIndexPath
    NSIndexPath *previousSelection = self.checkedIndexPath;
    NSArray *array = nil;
    if (nil != previousSelection) {
        array = [NSArray arrayWithObjects:previousSelection, indexPath, nil];
    } else {
        array = [NSArray arrayWithObject:indexPath];
    }

    self.checkedIndexPath = indexPath;

    [tableView reloadRowsAtIndexPaths:array withRowAnimation: UITableViewRowAnimationNone];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Part of code from cellForRowAtIndexPath
    NSString *cellID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        [cell autorelease];
    }

// some code for initializing cell content
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if(self.checkedIndexPath != nil && indexPath.row == self.checkedIndexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
} 

Upvotes: 6

Related Questions