Jak ustawić UILabel w UICollectionViewCell
Mam aplikację z UIViewController z UICollectionView IBOutlet w nim.
Komórka w UICollectionView to MyCustomCell i ma tę metodę ustawiającą UILabel:
-(void)setCellLabel:(NSString *)value{
NSLog(@"settinglabel");
cellLabel.text = @"hardcode"; //added for testing purposes
}
Komórka ma typ klasy właściwości identyfikujący ją jako MyCustomCell w serii ujęć i jej identyfikator kolejki. UIViewController przyjmuje protokoły źródła danych i delegata. IBOutlet UILabel ma swoje gniazdo cellLabel podłączone do storyboardu. Metody są zdefiniowane jako:
- (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;
}
Otrzymuję trzy komórki narysowane w moim widoku tabeli, odpowiadające 3 obiektom w mojej tablicy. Tablica zawiera po prostu obiekty łańcuchowe, które ustawiałem bezpośrednio za pomocą objectAtIndexPath, ale postanowiłem ustawić ją bezpośrednio na @ „hi”, ponieważ nie działała. Zmieniłem nawet wartość używaną w metodzie setCellLabel na wartość zakodowaną i w każdej komórce zachowuję domyślny ciąg „Label”.
Dlaczego etykieta CellLabel nie jest poprawnie ustawiona?