Search code examples
iosobjective-cuitableviewios6xcode4

Set checkmark in UITableView


I'm trying to set checkmark in rows which are selected in UITableView (placed in popup view). Actually I have something like this:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Check if current row is selected
    Boolean isNowChecked = NO;
    if([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) 
    {
        isNowChecked = YES;
    }

    if(isNowChecked) 
    {
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    }
    else 
    {
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    }
    return indexPath;
}

In result I'm able to select rows, for example I touch first item then I see checkmark. When I scroll down my table view, then I see that I have check also other items (number 17, 32). When I uncheck my first item, then every check also disappear.

Do you have any suggestions what could be reason of this situation, and how can I avoid it?


Solution

  • // Try this //in .h

    NSMutableArray *arr_fortable;
    

    //in .m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        arr_fortable = [[NSMutableArray alloc] init];
        for (int i =0; i<5; i++)
        {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:[NSString stringWithFormat:@"%d",i+1] forKey:@"data"];
            [dict setObject:@"0" forKey:@"checkmark"];
            [arr_fortable addObject:dict];
        }
    }
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [arr_fortable count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        // Configure the cell...
        cell.textLabel.text = [[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"data"];
        if ([[[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"checkmark"] integerValue] == 1)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([[[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"checkmark"] integerValue] == 1)
        {
            [[arr_fortable objectAtIndex:indexPath.row] setObject:@"0" forKey:@"checkmark"];
        }
        else
        {
            [[arr_fortable objectAtIndex:indexPath.row] setObject:@"1" forKey:@"checkmark"];
        }
        [self.tableView reloadData];
    }