向下滚动Tableview时,Checkmark重复

时间:2013-05-08 11:21:47

标签: iphone ios

我在tableview中显示某些信息。这些基本上都是考场。我正在使用这个逻辑来设置选中特定房间的复选标记。代码如下:

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

  }

当我向下滚动表格时,复选标记会随机重复任何单元格。请帮帮我,因为这个问题花了很多时间给我。

1 个答案:

答案 0 :(得分:3)

您必须清除可能已经放置的任何复选标记,因为UITableView会重复使用单元格并且不会自动执行此操作:

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