0

外部デリゲート コントローラーを持つテーブル ビューを作成していますが、ストーリーボードではなくコードで作成されています。セルに問題があります。実行がデリゲート メソッドに正しく到達しているにもかかわらず、セルが表示されません。

ビルド方法:

-(void)buildCategorias{

    CGRect twitterFrame = CGRectMake(105, 83, 600, 568);

    categoriasView = [[UIView alloc] initWithFrame:twitterFrame];

    CGRect scrollViewFrame = CGRectMake(105, 83, 400, 568);

    categoriasTableView = [[UITableView alloc] initWithFrame:scrollViewFrame];

    categoriasController = [[categoriasViewController alloc] init];

    categoriasController.categorias = [[NSArray alloc] initWithObjects:@"Gafas", @"Relojes", @"Pantalones", @"Deportivas", @"Cazadoras", nil];

    [categoriasTableView setDelegate:categoriasController];

    [categoriasTableView setDataSource:categoriasController];

    [self.categoriasView addSubview:categoriasTableView];

    [categoriasTableView reloadData];

    [self.view addSubview:categoriasView];



}

カスタム セル: categoriasCell.h

@interface categoriasCell : UITableViewCell{

    UILabel *title;

}

@property (nonatomic, strong) IBOutlet  UILabel *title;

@end

カテゴリCell.m

@implementation categoriasCell

@synthesize title;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

テーブル ビュー デリゲート:

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

    categoriasCell *cell = [self.tableView 
                      dequeueReusableCellWithIdentifier:@"categorias"];

    if (cell == nil) {
        cell = [[categoriasCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"categorias"];
    }

    cell.title.text = [categorias objectAtIndex:indexPath.row];


    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

    @end

テーブル ビューは空で、コンテンツはありません。

助けてくれて本当にありがとうございます

4

1 に答える 1

1

セルがまったくありませんか、それとも正しい数のセルがありますが、それらは空ですか?

セルがまったくない場合は、他の必要なデリゲート メソッド ( tableView:numberOfRowsInSection:) を実装したこと、および期待するセル数が実際に返されることを確認してください。

それ以外の場合は、NIB からセルをロードするコードが欠落している可能性があります (IBOutlet に IBOutlet があり、categoriasCell実際にビューにタイトル ラベルを追加するコードがない場合、セルが来ることを意図していると想定しています)。 NIB から)。セルの NIB をロードしてデリゲート メソッドで使用する方法については、Apple のドキュメントを参照してください。NIB からロードしない場合、テキスト フィールドは存在しないため、セルは空になります。

致命的ではないコーディング/スタイルの問題が 2 つあります。

  • UITableViewCellSelectionStyleNoneセルを初期化するときにスタイルとして渡しています。UITableViewCellStyleDefault引数は選択スタイルではなくセル スタイルであるため、ここで実際に使用する必要があります。
  • クラス名は (CategoriasCellの代わりにcategoriasCell) 大文字にする必要があります。
于 2012-07-14T23:15:13.587 に答える