Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 创建空集合视图单元格会引发错误_Ios_Objective C_Ios7_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

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文件