我不明白为什么故事板中无法识别自定义单元格。
TNTopStoriesCollectionViewController:
static NSString * const reuseIdentifier = @"TNTopNewsCellItem";
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TNTopStoriesCollectionViewCell *cell = (TNTopStoriesCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
[self preLoadImagesForCategory:_dataStore.fiveTopStories[indexPath.item] andImageView:cell.itemImage];
return cell;
}
#import <UIKit/UIKit.h>
@interface TNTopStoriesCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *itemImage;
@property (weak, nonatomic) IBOutlet UILabel *title;
@end
然而我在cellForItemAtIndexPath
方法中得到了这个例外:
[UICollectionViewCell itemImage]: unrecognized selector sent to instance
但为什么请?
更新
然而它仍然在同一地点失败。 :(
答案 0 :(得分:2)
这个细胞被称为&#34; Collection View Cell&#34;在 left 上,在场景中对象的层次名称列表中,并没有更改为&#34; Top Stories Collection View Cell&#34;表示你已经不将故事板中的类实际更改为TNTopStoriesCollectionViewCell。
(请注意集合视图控制器如何在 left 上列为Top Stories Collection View Controller。单元格应该发生同样的事情。)
你在左边看到的内容应该更像是这样:
(另外,您的标签出口在左侧被称为新闻标签的事实表明存在问题,因为在代码中,此标签被称为&#34; title&#34;,如果你已经正确地把它搞定了我希望Xcode在左边的列表中将其名称更改为&#34; title&#34 ;.
[我有一种模糊的感觉,你实际上可能有两个课程,TNTopStoriesCollectionViewCell和TNTopNewsCellItem,你们已经把他们弄糊涂了......]
答案 1 :(得分:0)
在故事板中,还有另一种创建自定义的方法 CollectionViewCell.That是单独创建新的 文件(CustomCellCollectionViewCell.h,CustomCollectionViewCell.m和 CustomCollectionViewCell.xib)通过IOS-&gt; Source-&gt; Cocoa Touch Class-&gt; UICollectionViewCell的SubClass并在Class中给出名称。后 您可以导入CustomCollectionViewCell的.h 你需要在viewDidLoad中注册它。
- (void)viewDidLoad
{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
UINib *cellNib = [UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil];
[collectionViewSelection registerNib:cellNib forCellWithReuseIdentifier:@"customCollectionCell"];
}
然后
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"customCollectionCell";
CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}