Rin Rin - 1 month ago 8
iOS Question

Checkmark in tableViewCell hidden when Scroll

When i scroll Table, checkmark is hide. I know because of Reusing Cell, but I dont know how to fix. Pls help me. Here is my code:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}

if (_btnCancel.hidden == NO) {
cell.accessoryType = UITableViewCellAccessoryNone;

} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

return cell;
}


I change check and uncheck in didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[dict setObject:courseStudent.id_user forKey:@"student_id"];

[studentDetail addObject:dict];

} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}

}
}

Answer

When you scroll table view, cellForRowAtIndexPath for particular cell will be called in which your are setting the accessoryType as None. Instead try like below.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[StudentTableViewCell alloc] init];
    }

    if (_btnCancel.hidden == NO) {
       TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
       if ([studentDetail containsObject:courseStudent]) {
           cell.accessoryType = UITableViewCellAccessoryCheckmark;
       else {
           cell.accessoryType = UITableViewCellAccessoryNone;
       }
    } else {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

    if (_btnCancel.hidden == NO) {
        if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
            cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
            TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
            [dict setObject:courseStudent.id_user forKey:@"student_id"];

            [studentDetail addObject:dict];

        } else {
            cellCheck.accessoryType = UITableViewCellAccessoryNone;
            [studentDetail removeObject: studentQuitArray[indexPath.row]];
        }

    }
}

Hope this will help.