我有一个包含一个 UICollectionView IBOutlet 的 UIViewController 应用程序。
UICollectionView 中的单元格是 MyCustomCell,它有一个设置其 UILabel 的方法:
-(void)setCellLabel:(NSString *)value{
NSLog(@"settinglabel");
cellLabel.text = @"hardcode"; //added for testing purposes
}
在Storyboard中,该单元格具有Class类型属性,标识为MyCustomCell,并设置它的dequeue identifier。UIViewController采用了数据源和委托协议。IBOutlet UILabel的cellLabel插座已连接到Storyboard。方法定义如下:
- (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;
}
我在我的tableview中画了三个单元格,对应于我的数组中的3个对象。该数组只包含字符串对象,我最初通过objectAtIndexPath直接设置它们,但是由于它不起作用,我决定直接将其设置为@"hi"。我甚至将setCellLabel方法中使用的值更改为硬编码值,但每个单元格仍然只得到了默认的“Label”字符串。
为什么cellLabel没有被正确设置?
[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier: CellIdentifier]
?如果你使用Storyboard,就不应该这么做。它会覆盖你在Storyboard中设置的内容。 - Patrick Tescher