UICollectionView IBOutlet を含む UIViewController を含むアプリがあります。
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 が正しく設定されていないのはなぜですか?