我正在使用带有自定义单元格 (xib) 的 UITableView。每个单元格都是标签和一个复选框(UIButton)。
我有 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;
}
在另一个类中,我检查复选框是否被选中:
- (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];
}
}
如何区分每个单元格以避免重复检查?
非常感谢! 谨致问候,
最佳答案
您需要在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:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 调用此方法,如下所示
- (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;
}
希望这有帮助。
谢谢。
关于iphone - UITableViewCell 滚动时复选框重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502479/