Search code examples
iosuitableviewcheckmark

checkmark does not appear on first cell


i need to add checkmark when we tap on each cell, and it must show when i return back to tableview. I tried with this code but its not working,

     - (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];
}

if (self.thread == nil) {
    cell.textLabel.text = @"Loading, please wait...";
} else if ([self.thread count] == 0) {
    cell.textLabel.text = @"No Results!";
} else {

    //
    NSDictionary *msg = [self.thread objectAtIndex:indexPath.row];
    NSLog(@"yourMutableDictionary - %@",msg);

    if (indexPath.row == 0) {
        cell.textLabel.text = [thread objectAtIndex:0];

    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ ", [msg objectForKey:@"id"]];

        if ([self.idSelected count] != 0) {
            if ([self.idSelected containsObject:[NSString stringWithFormat:@"%@", [msg objectForKey:@"id"]]]) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
    }

}

return cell;
 }

    #pragma mark - Table view delegate

   //In a xib-based application, navigation from a table can be handled in -   tableView:didSelectRowAtIndexPath:
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path
     {

if (self.thread == nil || self.thread.count == 0) {
    return;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];

    if (path.row == 0) {

        cell.accessoryType = UITableViewCellAccessoryCheckmark;// this checkmark not working 

    }
 else {
    NSDictionary *user = [self.thread objectAtIndex:path.row];
    NSString *uName = [NSString stringWithFormat:@"%@ %@", [user   objectForKey:@"first_name"], [user objectForKey:@"last_name"]];
    NSString *uId = [NSString stringWithFormat:@"%@",[user objectForKey:@"id"]];
    NSLog(@" click id%@", uId);


    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        [userNames removeObject:uName];
        [userIds removeObject:uId ];

        cell.accessoryType = UITableViewCellAccessoryNone; // this check mark is working perfectly
    } else {
        [userNames addObject:uName];
        [userIds addObject:uId];
        cell.accessoryType = UITableViewCellAccessoryCheckmark; // this check mark is working perfectly

    }

}

}

first cell check mark is not working but others working perfectly. I want to add check mark on first cell too, how its possible


Solution

  • Use below code to display checkmark on multiple table view cell :

     - (void)viewDidLoad
        {
            [super viewDidLoad];
    
            self.arraySelectedCell = [NSMutableArray array];
        }
    
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {        
            if ([self.arraySelectedCell containsObject:indexPath])
            {
              cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else
            {
              cell.accessoryType = UITableViewCellAccessoryNone;
    
            }
    
            // do here..
    
            return cell;
        }
    
         - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
            //the below code will allow multiple selection
            if ([self.arraySelectedCell containsObject:indexPath])
            {
              [self.arraySelectedCell removeObject:indexPath];
            }
            else
            {
               [self.arraySelectedCell addObject:indexPath];
            }
    
            [tableView reloadData];
        }
    

    happy coding!