我正在使用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];
}
}
如何区分每个单元以避免重复检查?
非常感谢! 此致敬礼,
把代码od cellforrowAtindexpath方法 – iPatel 2013-03-19 14:41:10
完成;)谢谢:) – Lapinou 2013-03-19 14:46:28