简体   繁体   中英

Checkmark is repeating when scrolling down the Tableview

I am displaying certain information in a tableview. These are basically Exam rooms. I am using this logic in order to put the check marks for selecting the particular room. The code is as follows:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
      *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    // intializing tableview cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier] autorelease];
    }


    // Setting tableviewcell title from Room array.

    cell.textLabel.text = [NSString stringWithFormat:@"%@",[[arr_roomList
     objectAtIndex:indexPath.row] valueForKey:@"description"]];

    /* checking the condition if checkedIndexPath is != null.
     means at first time checkedIndexPath= null.*/
    if (self.checkedIndexPath) {

        /* checking the condition if current cell is selected then
         we have to show the UITableViewCellAccessoryCheckmark (checkmark on right side of the
        cell).*/
        if([self.checkedIndexPath isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    else{
        if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"resource_id"]
       isEqualToString:self.str_selected_resourceId]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }




    // This Method will set the Background and Selected Background Image for the cell.
    [[AppDelegate sharedInstance] SetBackgroundImageForcell:cell];



    if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"rec_type"] isEqualToString:@"R"]) {
        cell.backgroundColor = [UIColor grayColor];
    }

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    /* checking the condition if checkedIndexPath is != null.
     means at first time checkedIndexPath= null.*/
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;

    [self changeRoomWithResourceId:[[arr_roomList objectAtIndex:indexPath.row] 
    valueForKey:@"resource_id"]];

  }

As I am scrolling down the table the check marks are repeating themselves for any cell randomly. Please Help me as this issue is taking lot of time for me.

You have to clear out any checkmarks that might be placed already, because UITableView reuses cells and does not do it automatically:

    // ...
    if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"resource_id"]
    isEqualToString:self.str_selected_resourceId]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    // add this
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM