3

UICollectionView IBOutlet を含む UIViewController を含むアプリがあります。

MyCustomCell アウトレットが接続されました MyCustomCell ReuseID MyCustomCell クラス ID

UICollectionView のセルは MyCustomCell であり、このメソッドで UILabel を設定します。

-(void)setCellLabel:(NSString *)value{
    NSLog(@"settinglabel");
    cellLabel.text = @"hardcode"; //added for testing purposes
}

セルには、ストーリーボードで MyCustomCell として識別されるプロパティ クラス タイプと、そのデキュー識別子もあります。UIViewController は、データソース プロトコルとデリゲート プロトコルを採用しています。IBOutlet UILabel には、ストーリーボードに接続された cellLabel アウトレットがあります。メソッドは次のように定義されます。

- (void)viewDidLoad{
    [super viewDidLoad];
    self.restNames = @[@"Orange",@"Naranja",@"Narnia"];

    [self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"MyCustomCellID"];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"%d",[self.restNames count]);
    return [self.restNames count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];


    [cell setCellLabel:@"hi"];
    NSLog(@"fitsname %@",[self.restNames objectAtIndex:indexPath.row]);


    return cell;
}

配列内の 3 つのオブジェクトに対応する 3 つのセルがテーブルビューに描画されます。配列には、objectAtIndexPath で直接設定していた文字列オブジェクトが含まれているだけですが、機能していなかったため、@"hi" に直接設定することにしました。setCellLabel メソッドで使用される値をハードコードされた値に変更し、各セルで「ラベル」のデフォルト文字列だけを取得し続けます。

cellLabel が正しく設定されていないのはなぜですか?

4

3 に答える 3

5

Storyboard を使用している場合、既定のテンプレートは間違っています。

[self.collectionView registerClass:[UICollectionView class] forCellWithReuseIdentifier:CellIdentifier];

しかし、ストーリーボードはすでにそれを登録しています。したがって、基本的にこれは、カスタムのストーリーボード ベースを作成するために必要なことですUICollectionView

  1. UICollectionViewControllerサブクラスを作成する
  2. をクリックしてストーリーボードにドラッグし、UICollectionViewController必要に応じて構成を変更します
  3. UICollectionViewCellサブクラスを作成する
  4. ストーリーボードにセル クラスを設定し、その再利用識別子を設定します。Ctrl キーを押しながら必要なアウトレットをドラッグします。
  5. を取り外します[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: CellIdentifier];
  6. UICollectionViewサブクラスに静的再利用識別子を設定する
于 2014-07-04T07:26:48.317 に答える