0

カスタムセル(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];
    }

}

チェックを繰り返さないように各セルを区別するにはどうすればよいですか?

どうもありがとう!よろしくお願いします、

4

2 に答える 2

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:(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;
}

お役に立てれば。

ありがとう。

于 2013-03-19T15:01:00.187 に答える
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;
于 2013-03-19T14:47:28.563 に答える