当我滚动tableview时,UITableView会随机勾选UITableViewCellAccessoryCheckmark单元格

时间:2013-12-26 06:45:15

标签: uitableview

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"LeftTable";
   // LabDetailsVO* objLabDetailsVO=(LabDetailsVO* )dictData;



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    NSArray *arrCurrentDataSource = [arrDataSource objectAtIndex:tableView.tag];
    cell.textLabel.text = [arrCurrentDataSource objectAtIndex:indexPath.row];

    cell.textLabel.font = FONT_ORDERS_LEFT_CELL;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}




/**********************************************************************/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell;
        int lastSelectedIndexPath = [[arrSelectedIndexPaths objectAtIndex:tableView.tag] intValue];
        if (lastSelectedIndexPath != -1) {
            cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:lastSelectedIndexPath inSection:0]];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        [arrSelectedIndexPaths replaceObjectAtIndex:tableView.tag withObject:[NSString stringWithFormat:@"%d", indexPath.row]];
        cell = [tableView cellForRowAtIndexPath:indexPath];

            cell.accessoryType = UITableViewCellAccessoryCheckmark;

        //Notify the parent controller about the new value selected by the user.
        if ([idParentDelegate respondsToSelector:@selector(popOverSelectionNotifier: value: )]) {
            [idParentDelegate popOverSelectionNotifier:tableView.tag value:[NSString stringWithFormat:@"%d", indexPath.row]];


        }

        else {
            NSLog(@"Error : %@ - Does not protocol(OrdersDetailPopViewControllerSelectionDelegate) method (popOverSelectionNotifier: value:)", NSStringFromClass([idParentDelegate class]));
        }
    }

1 个答案:

答案 0 :(得分:0)

表视图在滚动时重用单元格,重置 cellForRowAtIndexPath

中的设计和值
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       ........
       if (cell == nil) 
      {
         ..........
         ..........
      }

      if(lastSelectedIndexPath != -1)
      {
         cell.accessoryType = UITableViewCellAccessoryNone;
      }
      else
      {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
      }

     return cell;
    }