0

テーブルビューのスクロール中に EXC_BadAccess エラー メッセージが表示されます。

以下は、cellForRowAtIndexPath で行ったコードです。

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

    static NSString *CellIdentifier=@"customCellHistory";

    customCellHistory *cell=(customCellHistory*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@"customCellHistory" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]] ) {
                cell=(customCellHistory*)currentObject;
                break;
            }
        }
    }

    cell.lb11.text=[cellArray1 objectAtIndex:indexpath.row];
    cell.lbl2.text=[cellArray2 objectAtIndex:indexpath.row];

    return cell;
}

上記のコードの間違いが原因で問題が発生していることがわかります。

上記のコードで CustomCell を使用して、カスタマイズされたセルを表示しました。

このコードで何が間違っているのか誰か教えてもらえますか

4

4 に答える 4

1

次のコードを試してみてset the cell identifierください。カスタム XIB を忘れずにcustomCellHistory

頂点で

#import "customeCellHistory.h"

それから

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

    static NSString *cellIdentifier = @"customCellHistory";

    customCellHistory *cell = (customCellHistory *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCellHistory" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.lb11.text=[cellArray1 objectAtIndex:indexpath.row];
    cell.lbl2.text=[cellArray2 objectAtIndex:indexpath.row];

    return cell;
}
于 2013-05-08T15:12:58.283 に答える
0
if (cell == nil) {
    NSArray *topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@"customCellHistory" owner:self options:nil];

そのコードは必要ありません。先に、テーブル ビューに nib を登録するだけです。

[self.tableView registerNib:[UINib nibWithNibName:@"customCellHistory" bundle:nil]
 forCellReuseIdentifier:@"customCellHistory"];

を呼び出すとdequeueReusableCellWithIdentifier、再利用パイルに予備のセルがない場合、ペン先がロードされ、セルが配信されます。これにより、セルが一貫して存在し、それが適切な種類のセルであることが保証されます。フレームワークが提供するメソッドを使用します。

于 2013-05-08T15:01:31.333 に答える