用xib加载时,需要注意的地方。
// 把UICollectionView注册近复用池里(千万不能忘)
[_collectionViewregisterClass:[DetailManageCellclass] forCellWithReuseIdentifier:cellIdentifier];
#pragma mark - UICollectionViewDataSource 代理方法 -
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
DetailManageCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:cellIdentifierforIndexPath:indexPath];
return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 6;
}
#pragma mark - UICollectionViewDelegate 代理方法 -
#pragma mark - UICollectionViewLayout -
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
returnUIEdgeInsetsMake(0,20, 0, 20);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
returnCGSizeMake(imageViewW,imageViewW);
}
UICollceionViewCell中加载xib(至于UICollectionView为什么要写这一方法加载,还不太清楚,只是不这样写,就没有显示)
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
// 初始化时加载xib文件
NSArray *arrayOfViews = [[NSBundlemainBundle]loadNibNamed:@"DetailManageCell"owner:self options:nil];
// 如果路径不在,
if (arrayOfViews.count <1) {
returnnil;
}
// 如果xib中view不属于collectionView类,
if (![[arrayOfViewsobjectAtIndex:0]isKindOfClass:[UICollectionViewCellclass]]) {
returnnil;
}
// 加载xib
self = [arrayOfViewsobjectAtIndex:0];
}
returnself;
}