2013-03-19 56 views
0

我正在使用UITableView自定义单元格(xib)。每个单元格都是lables和一个复选框(UIButton)。UITableViewCell复选框重复滚动

我在每个部分有2个部分和4个单元格。如果我检查第一部分的第一个单元格,第二部分的第一个单元格也会被检查,而我不想要。问题:dequeueReusableCellWithIdentifier:CellIdentifier。

我想保持我的单元格标识符是静态的。

我该如何解决这个问题?

这是我的数组的初始化(对于我的单元的内容):

for(int i=0; i<NUMBER_OF_CELL; i++){ 

    Account *model = [[Account alloc]init]; 

    [model setAccountName:[NSString stringWithFormat:@"Account %d",i]]; 
    [model setAccountNumber:[NSString stringWithFormat:@"Number %d",i]]; 

    [_accountArray addObject:model]; 

} 

设置内容:

[[cell accountLabel] setText:_model.accountName]; 
[[cell accountNumberLabel] setText:_model.accountNumber]; 

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    _model = [_accountArray objectAtIndex:indexPath.row]; 

    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AccountCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 
    // configure cell 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    [[cell accountLabel] setText:_model.accountName]; 
    [[cell accountNumberLabel] setText:_model.accountNumber]; 
    // checkbox ? 

    if(cell.isChecked){ 

     NSLog(@"Checked"); 
    }else{ 
     NSLog(@"No checked"); 
    } 

    return cell; 
} 

在另一个c lass,我检查复选框是否被选中:

- (IBAction)checkbox:(id)sender { 

    NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self]; 

    if(self.isChecked == NO) 
    { 
     self.isChecked = YES; 
     [_checkbox setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];   
    } 
    else{ 
     self.isChecked = NO; 
     [_checkbox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; 
    } 

} 

如何区分每个单元以避免重复检查?

非常感谢! 此致敬礼,

+0

把代码od cellforrowAtindexpath方法 – iPatel 2013-03-19 14:41:10

+0

完成;)谢谢:) – Lapinou 2013-03-19 14:46:28

回答

0

我认为你最好使用选定的状态为按钮来跟踪它是否被检查。看起来你已经创建了一个“AccountCell”类,所以你应该能够简单地用插座在AccountCell类的复选框,如:

@interface AccountCell : UITableViewCell 
... 
@property IBOutlet UIButton *checkBox; 

现在,你可以设定正常/检查在AccountCell的viewDidLoad方法的按钮图像(因为这并不需要做的每一次状态的变化):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [_checkBox setImage:[UIImage imageNamed:"checkbox.png" forState:UIControlStateNormal]; 
    [_checkBox setImage:[UIImage imageNamed:"checkbox_checked.png" forState:UIControlStateSelected]; 
} 

在你cellForRowAtIndex路径可以确保复选框通过使用返回的细胞清除:

cell.checkBox.selected = NO; 

然后在复选框的“行动”代码,你可以使用以下方法来打开和关闭的复选框:

self.selected = !self.selected; 
+0

是的,我理解这个概念,但我不知道如何实现它:/ – Lapinou 2013-03-19 14:49:20

+0

编辑与更好的新的更好的答案,并应该解决您的问题 – cdemiris99 2013-03-19 15:29:25

1

你需要写继AccountCell.h/AccountCell.m

方法
- (void)resetCell { 

    //Reset Your cell content to default. So that you can reuse the cell. 
    NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self]; 


     self.isChecked = NO; 
     [_checkbox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; 

    //other resettings... 
} 

然后你就可以调用从(UITableViewCell的*)的tableView这种方法:如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    _model = [_accountArray objectAtIndex:indexPath.row]; 

    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AccountCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 
    // configure cell 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    //reset your cell's content. 
    [cell resetCell]; 

    //perform your task below 
    [[cell accountLabel] setText:_model.accountName]; 
    [[cell accountNumberLabel] setText:_model.accountNumber]; 
    // checkbox ? 

    if(cell.isChecked){ 

     NSLog(@"Checked"); 
    }else{ 
     NSLog(@"No checked"); 
    } 

    return cell; 
} 
(NSIndexPath *)indexPath:(UITableView的*)的tableView的cellForRowAtIndexPath

希望这有助于。

谢谢。

+0

是的,它可以工作,但是如果复选框被选中的单元格不可见(滚动后),则先前选中的复选框未被选中:/ – Lapinou 2013-03-19 15:11:46

+1

每当单元格离屏并返回到屏幕上时,我们将重置单元格的显示resetCell(这是不包含任何值的单元格的默认显示)。重置后,我们有责任将复选框标记为已选中。我的意思是你必须维护数组中所有行的状态(或者你正在使用的任何类型),并且当cellForRowAtIndexPath被调用时,你必须查看这个数组的状态,并且以编程方式更新单元格的内容(在你的情况下,以编程方式检查复选框) – Kunal 2013-03-19 15:22:42