Ant*_*ite 28 objective-c ios uicollectionview
首先尝试使用集合视图并遇到此错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使类型的视图出列:具有标识符的UICollectionElementKindCellCell - 必须为标识符注册nib或类或在故事板中连接原型单元'
代码非常简单,如下所示.我不能为我的生活弄清楚我错过了什么.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor]; 
return cell;
}
集合视图控制器是使用nib创建的,委托和数据源都设置为文件的所有者.
View Controller的头文件也非常基本.
@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end
jrt*_*ton 35
要点:在调用此方法之前,必须使用registerClass:forCellWithReuseIdentifier:或registerNib:forCellWithReuseIdentifier:方法注册类或nib文件.
Koh*_*ami 26
您需要在dequeueReusableCellWithReuseIdentifier的参数和UICollectionViewCell的属性之间使用相同的标识符.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];

补充@jrtuton写的......你需要的是:
1)注册你的nib文件,用你的标识符"连接"它:
//MyCollectionView.m
- (void) awakeFromNib{
 [self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil]   forCellWithReuseIdentifier: @"MyCellIdentifier"];
}
2)使用您的标识符从笔尖加载自定义单元格:
//MyCollectionView.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath  *)indexPath {
    MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath];
}
3)使用始终静态NSString*来避免在您的应用程序中再次使用相同的标识符.
小智 6
雨燕5
1)确保您有一个正确的 HEADER 双端队列,您可能使用普通单元的常规双端队列。
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath)
        return header
    }
2)双重检查注册(ViewDidLoad)
collectionView.register(HeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
| 归档时间: | 
 | 
| 查看次数: | 47625 次 | 
| 最近记录: |