0

カスタムのテーブルビューセルを作成しました。クラスには 3 つのラベルがあります。マスター ビュー コントローラー テンプレートを使用して開始し、ストーリーボードの既定の tableviewcell を新しいカスタム セルを参照するように変更し、タイプをカスタムに、識別子を 'CustomTableCell' に変更しました。cellForRowAtIndexPath メソッドも次のように変更しました...

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

    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    Item *currentItem = _objects[indexPath.row];
    cell.nameLabel.text = [currentItem name];
    cell.vegLabel.text = @"V";
    return cell;
}

カスタム セル ヘッダー ファイル

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *vegLabel;
@property (nonatomic, weak) IBOutlet UILabel *priceLabel;

@end

ストーリーボードでは、すべてが適切に接続されているようです。デバッグすると、セルにカスタム セルのプロパティがあることがわかります。しかし、アプリケーションを実行すると、各行が空白になります。tableviewcell は、ストーリー ボードで正しい識別子を使用しています。何が欠けているのかわかりません。どんな助けでも大歓迎です。ありがとう。

ストーリーボードの識別子 ストーリーボードでの接続

4

1 に答える 1

1

メインバンドルからカスタム セルをロードしていません。したがって、ロードする必要があります。

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

    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // Add this line in your code
    cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableCell" owner:self options:nil]objectAtIndex:0]; 

    if (!cell)
    {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    Item *currentItem = _objects[indexPath.row];
    cell.nameLabel.text = [currentItem name];
    cell.vegLabel.text = @"V";
    return cell;
}
于 2013-05-16T08:37:49.207 に答える