Ios 创建空集合视图单元格会引发错误
我正在将Ios 创建空集合视图单元格会引发错误,ios,objective-c,ios7,uicollectionview,uicollectionviewcell,Ios,Objective C,Ios7,Uicollectionview,Uicollectionviewcell,我正在将UICollectionView与自定义UICollectionViewCell一起使用 我使用以下方法创建空单元格: [[MyCell alloc] init]; 和编译器抛出错误: Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] 我在下面的viewdiload:方法中进行了尝试: [self.co
UICollectionView
与自定义UICollectionViewCell
一起使用
我使用以下方法创建空单元格:
[[MyCell alloc] init];
和编译器抛出错误:
Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
我在下面的viewdiload:
方法中进行了尝试:
[self.collection registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
MyCell *cell = [[MyCell alloc] init];
然后在-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath方法中:
[self.collection registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
MyCell *cell = [[MyCell alloc] init];
如果不存在相应的视图,则必须告诉集合视图如何创建该视图。为此,必须在collection视图中注册类或nib文件
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
在实现文件中创建一个常量NSString
标识符
static NSString * const CustomClassCellIdentifier = @"Cell";
然后在viewDidLoad:
[self.collectionView registerNib:[UINib nibWithNibName:@"CustomClassCell" bundle:nil] forCellWithReuseIdentifier:CustomClassCellIdentifier];
//[self.collectionView registerClass:[CustomClassCell class] forCellWithReuseIdentifier:CustomClassCellIdentifier];
然后在dataSource
方法中
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomClassCell *customCell =
[collectionView dequeueReusableCellWithReuseIdentifier:CustomClassCellIdentifier
forIndexPath:indexPath];
return customCell;
}
这可能是因为您没有使用任何类型的reuseIdentifier
@Morpheus,那么您的建议是什么呢?我添加了[self.collection registerClass:[MyCell class]forCellWithReuseIdentifier:@“cellIdentifier”]代码>在viewDidLoad:
中,但仍会因相同的错误而崩溃。我已经在故事板中创建了这个单元格,并为它创建了类。我需要空单元格。使用dequeueReusableCellWithReuseIdentifier:
将为单元格提供一些已设置的值。是否使用情节提要?编辑的答案现在检查,如果您使用的是自定义单元格的nib文件