Галочка не появляется в первой ячейке

Мне нужно добавить галочку, когда мы нажимаем на каждую ячейку, и она должна отображаться, когда я возвращаюсь обратно в просмотр таблицы. Я пытался с этим кодом, но он не работает,

     - (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

    }

}

}

Первая ячейка галочка не работает, но другие работают отлично. Я хочу добавить галочку на первую ячейку, насколько это возможно

1 ответ

Решение

Используйте приведенный ниже код для отображения галочки в ячейке просмотра нескольких таблиц:

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

Удачного кодирования!

Другие вопросы по тегам