2014-01-23 116 views
0

我知道这里有很多类似的问题,但没有解决我的问题。在我的UITableView中有很多部分,当我单击/勾选单元格中的某些按钮并向下滚动时,复选标记在再次滚动时消失。这是我编码..滚动UITableView重置单元格的复选框按钮状态

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellId = @"CheckBoxedCell"; 
    // NSString *cellId = [NSString stringWithFormat:@"Section:%d Row:%d",indexPath.section,indexPath.row]; 
    CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId]; 

    if(!cell) 
    { 
     NSArray *nib; 
     if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
     { 
      nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass" owner:self options:nil]; 
     } 
     else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     { 
      nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass_iPad" owner:self options:nil]; 
     } 
      for (id object in nib) 
      { 
       if([object isKindOfClass:[CheckBoxedCellClass class]]) 
       { 
        cell = (CheckBoxedCellClass *)object; 
        break; 
       } 
      } 

      cell = [nib objectAtIndex:0]; 

    } 

     SaveCheckBoxedView *saveContact; 
     if(isFiltered == YES) 
     { 
      saveContact = [filterdArray objectAtIndex:indexPath.row]; 
      cell.nameLabel.text = saveContact.nameString; 
     } 
     else 
     { 
      saveContact = [mutableArray objectAtIndex:indexPath.row]; 
      cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 
     } 


     cell.companyLabel.text = saveContact.companyString; 
     cell.invIdLabel.text = [NSString stringWithFormat:@"%@", saveContact.invitId]; 

     //set fonts 
     if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
     { 

      [cell.companyLabel setFont:[UIFont italicSystemFontOfSize:10.0]]; 
     } 
     else 
     { 

      [cell.companyLabel setFont:[UIFont italicSystemFontOfSize:14.0]]; 
     } 



    //handling check box 

    NSInteger rowNumber = 0; 
    for(NSInteger i = 0; i < indexPath.section ; i++) 
    { 
     rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i]; 
    } 

    rowNumber += indexPath.row; 


    UIButton *checkBox; 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    { 
     checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)]; 
    } 
    else 
    { 
     checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)]; 
    } 

    [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal]; 
    [checkBox addTarget:self action:@selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside]; 

    checkBox.tag = rowNumber; 

    [cell.contentView addSubview:checkBox]; 

    // handle check box view reset when scrolled 

    for(NSIndexPath *path in checkedIndexPaths) 
    { 
     if(path.section == indexPath.section && path.row == indexPath.row) 
     { 
      if(!checkedIndexPaths) 
      { 
       checkedIndexPaths = [[NSMutableSet alloc] init]; 
       [checkedIndexPaths addObject:[NSNumber numberWithInt:rowNumber]]; 
      } 
     } 
    } 

    return cell; 
} 


-(void)checkBoxClicked:(id)sender event:(id)event 
{ 

    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableViewContact]; 
    NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint: currentTouchPosition]; 
    NSLog(@"value of indexPath.section %d ,indexPath.row %d",indexPath.section,indexPath.row); 

    UIButton *tappedButton = (UIButton*)sender; 
    NSLog(@"Tag number = %d", [sender tag]); 


    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkBox.png"]]) 
    { 
     [sender setImage:[UIImage imageNamed: @"checkBoxMarked.png"] forState:UIControlStateNormal]; 
     if(isFiltered == YES) 
     { 
      NSString *addId = [filteredArrayOfIds objectAtIndex:indexPath.row]; 
      NSLog(@"filterd id = %@", addId); //get filtered array here 
      [arrayOfIds addObject:addId]; 
     } 
     else 
     { 
      NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag]; 
      NSLog(@"Tagged checked button id = %@", finalIntId); 
      [arrayOfIds addObject:finalIntId]; 
     } 

    } 
    else 
    { 
     [sender setImage:[UIImage imageNamed:@"checkBox.png"]forState:UIControlStateNormal]; 
     NSLog(@"UnChecked"); 

     [arrayOfIds removeObjectAtIndex:tappedButton.tag]; 
    } 

} 

回答

0

如果我没有弄错,你从来没有设置checkBox.checked?

0

当您向上和向下滚动时,只有可见的单元格将被出列和配置。因此,无论何时向下滚动整页,然后重新生成单元格都将被重新创建(您可以在cellForRowAtIndexPath的入口点NSLog查看我的意思)。

在此方法中重新创建按钮。请记住,表视图单元格是缓存因此,如果您创建了一个单元格,它可能会在需要时再次使用,因此只有在第一次创建单元格时才创建按钮,而不是每次调用cellForRowAtIndexPath时都创建该按钮。

如果使用自定义单元格可以添加boolean成员,表明细胞已经创建并没有必要再添新创建的按钮,将隐藏你原来当你[cell.contentView addSubview:checkBox];(或更好的将其添加但在这个自定义单元格的init方法中做按钮创建将更加容易和正确)

0

我用来实现与按钮类似的东西。因此,在单元格内部,您可以定义一个函数,用于更改复选框的背景或基于模型的复选框的状态。

-(void) setModel:(Model *)model { 

    [self.button setSomeStyleBasedOnYourModel];   
} 

如果在故事板的tableview中有该复选框,则可以将故事板中的目标链接到控制器。

- (IBAction)doTaskButtonPressed:(UIButton *)sender { 
    [super doTaskButtonPressed:sender]; 
}