1

UITableView の新しい registerClass メソッドを使用して問題が発生しています。セルをうまく登録してから、セルを作成したいときにこれを行います:

static NSString *imageIdentifier = @"image";
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:imageIdentifier];

if (!cell) {
    cell = [[CustomCell alloc] initWithQuestion:self.question reuseIdentifier:imageIdentifier];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

これは現代的な方法ではないかもしれませんが、私が以前に行った方法です。問題は、新しい registerClass メソッドがキューにセルがない場合に新しいセルを作成するため、if (!aCell) チェックが失敗し、セルが正しく構築されないことです。

正しくデキューするためにこの新しいアプローチを使用していませんか?

4

2 に答える 2

5

prepareForReuse1) UITableViewCellサブクラスのメソッドでセル(この場合はselectionStyle)を設定します。

2)デリゲートメソッドのdequeueReusableCellWithIdentifier:呼び出し後にセルの内容を設定します。tableView:cellForRowAtIndexPath:

dequeueReusableCellWithIdentifier:registerClass:対応する識別子で呼び出した場合は、常にセルを返します。

于 2012-12-13T07:32:45.840 に答える
1

新しいメソッド を使用dequeueReusableCellWithIdentifier:forIndexPath:し、if 句を省略します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    // configure cell here
    return cell;
}

クラスまたは xib を登録するときに、同じ再利用識別子を使用します。

于 2012-12-13T07:18:39.483 に答える