カスタムセル(xib)でUITableViewを使用しています。各セルはラベルとチェックボックス(UIButton)です。
各セクションに2つのセクションと4つのセルがあります。最初のセクションの最初のセルをチェックすると、2番目のセクションの最初のセルもチェックされるので、必要ありません。問題: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];
}
}
チェックを繰り返さないように各セルを区別するにはどうすればよいですか?
どうもありがとう!よろしくお願いします、