此問題適用於tableViews和collectionViews。爲什麼一個單元格有效,但其元素在創建後爲零
讓我們談談使用原型單元的集合視圖。
假設原型單元格被分配給MyCollectionViewCell
類。
假設這個類有兩個屬性:countryName
和countryImage
(分別是一個label和一個imageView)。
爲什麼我不能在此方法中爲名稱和國家分配值?什麼是解決這個問題的最好方法?
在此方法...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSArray *section = self.arrayData[indexPath.section];
NSDictionary *itemOnSection = section[indexPath.row];
// Configure the cell
UIImage *image = [UIImage imageNamed:itemOnSection[@"image"]];
NSString *name = itemOnSection[@"name"];
cell.countryImage.image = image;
cell.countryName.text = name;
return cell;
}
究竟會用這種方法出現的情況是細胞將是一個有效的對象,但cell.countryImage
和cell.countryName
將nil
。
我不明白的是:如果單元格存在並且是根據調試器的有效對象,爲什麼它的元素沒有被初始化。什麼是解決這個問題的最好方法?
這是MyCollectionViewCell.h
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *countryImage;
@property (weak, nonatomic) IBOutlet UILabel *countryName;
@end
的出口被連接到所述原型細胞和原型細胞中的元素具有此類分配。
MyCollectionViewCell.m沒有任何內容。
好的,但細胞不是零。無是單元格的標籤和imageView。這是我不明白的。如果單元格對象爲何它的屬性爲零? – SpaceDog 2014-10-06 11:00:42
顯示你的MyCollectionViewCell – 2014-10-06 11:01:23
我已經添加了更多代碼的問題,但它可能是不可見的,直到有更多的信譽的人批准它。我所擁有的就是連接到MyCollectionViewCell類的屬性和分配給該類的原型單元格的imageView和countryName標籤的出口。 – SpaceDog 2014-10-06 11:23:42