当uitableview上下滚动时,复选框状态会发生变化

时间:2012-01-18 18:35:06

标签: iphone ios uitableview uibutton

我有一个tableview,其中很多条目分为多个部分,里面有多行。我有一个按钮,每个行都有一个复选框。按下时,可以激活/禁用复选框(按钮)。我正在根据状态在按钮(复选框)上加载图像,无论是否已选中。一切都很顺利,直到我尝试向下拖动我的UITableview然后向上,只是为了找到一些自动取消选中的已选中复选框。我有点认为,这是由于使用

引起的
 dequeuereusablecellwithidentifier

我想避免我的tableview的这种奇怪的行为。在这种情况下需要帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    UIButton *checkBox = [[UIButton alloc] init];
    checkBox.tag = contact.contactID;
    [cell.contentView addSubview:checkBox];
    [checkBox setFrame:CGRectMake(6,14,20,20)];
    [checkBox release];
}
UIButton *checkBox = (UIButton *)[cell.contentView viewWithTag:contact.contactID];

if(isActivDeactivButton)
{
    [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
   }
else{
   [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];
 }
return cell;
}

3 个答案:

答案 0 :(得分:2)

确保-tableView:cellForRowAtIndexPath:的逻辑设置如下:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // this part is called when the cell is created anew
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // add new components here (this is where you'd add your checkbox control, etc)
    }

    // change the state of components here (this is where you'd make sure your checked property is set correctly

    return cell;
 }

使用dequeueReusableCellWithIdentifier:是内存管理的绝佳做法,因此您需要确保这一点。

答案 1 :(得分:2)

// Add following methods def in viewController .h 

  -(void)checkButtonPressed:(id)sender;
  -(void)addSelectedCheckBoxTag:(int)value;
  -(void)deleteSelectedCheckBoxTag:(int)value;
  -(BOOL)isSelectedCheckBox:(int)value;

//  add target to the check button in cellForRowAtIndexPath -

    [checkBox addTarget:self action:@selector(checkButtonPressed:) forControlEvents:UIControlStateNormal];

//  Ad following methods in viewController .m where tabelView

  -(void)checkButtonPressed:(id)sender
  {
   UIButton *checkBox=(UIButton*)sender;
   if(checkBox.selected)
    {
     checkBox.selected=false;
     [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
     [self deleteSelectedCheckBoxTag:checkBox.tag];
     NSLog(@"unselected ..");
    }
  else
    {
    checkBox.selected=true;
    [self addSelectedCheckBoxTag:checkBox.tag];
    [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];      
    NSLog(@"selected..");
  }    

 }

   // Add element in to array if already present forget   

  -(void)addSelectedCheckBoxTag:(int)value
     {
      int flag=0;

    for(int i=0;i<[arrayForTag count];i++)
     {        
       if([[arrayForTag objectAtIndex:i] intValue]==value)
       flag=1;
     }
   if(flag==0)
   [arrayForTag addObject:[NSString stringWithFormat:@"%d",value]];
   }

   // delete element add in array if present 

   -(void)deleteSelectedCheckBoxTag:(int)value
    {

   for(int i=0;i<[arrayForTag count];i++)
     {        
      if([[arrayForTag objectAtIndex:i] intValue]==value)
        [arrayForTag removeObjectAtIndex:i];
     }
    }

   // For take is selected or not from array -

-(BOOL)isSelectedCheckBox:(int)value
  {
    for(int i=0;i<[arrayForTag count];i++)
     {        
       if([[arrayForTag objectAtIndex:i] intValue]==value)
         return true;
      }
    return false;
  }

  // In cellForRowAtIndexPath  replace following code 

     if(isActivDeactivButton)
      {
      [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
      }
 else{
     [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"]  forState:UIControlStateNormal];
   }

    // with following code - 

    if(![self isSelectedCheckBox:checkBox.tag])
     {
       [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
     }
    else{
       [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];
        }

答案 2 :(得分:0)

我替换了

checkBox.tag = contact.contactID;

checkBox.tag = 111;

这解决了问题,因为我的contactID值在滚动期间发生了变化。