16

次のエラーが表示されます。

*キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了します。理由: '識別子 FontCell を持つセルをデキューできません - 識別子のペン先またはクラスを登録するか、ストーリーボードでプロトタイプ セルを接続する必要があります'

何が間違っているのか正確にはわかりません。セル識別子を設定し (Interface Builder を介して作成されていないため、プログラムで)、デリゲート メソッドで実行する必要があると思っていたすべてのことを行いますが、UITableView をロードしようとすると、まだエラーが発生します。

関連するコードは次のとおりです (カスタマイズ オプションのために UITableViewCell をサブクラス化したことに注意してください)。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.fonts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"FontCell";

    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (!cell) {
        cell = [[FontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FontCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    int row = indexPath.row;

    cell.fontFamilyLabel.text = self.fonts[row];

    return cell;
}

そして、サブクラス化された UITableViewCell (FontCell) で変更した唯一のメソッドは次のとおりです。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fontFamilyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
        self.fontFamilyLabel.textAlignment = NSTextAlignmentCenter;

        [self.contentView addSubview:self.fontFamilyLabel];
    }
    return self;
}

私は正確に何を間違っていますか?

4

4 に答える 4

4

dequeueReusableCellWithIdentifier:forIndexPath:メソッドを使用しています。そのメソッドのドキュメントには、次のように書かれています。

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

だからここに

[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"];
于 2013-04-02T20:06:25.763 に答える
2

私もそのような問題を抱えていましたが、私が見つけた解決策は次のとおりです。

Project Navigator に移動し、「ViewController.h」を選択します。追加

 <UITableViewDelegate, UITableViewDataSource>

「UIViewController」の後。

于 2013-05-08T05:31:31.267 に答える