UICollectionView单元笔尖未加载
问题描述:
我使用允许对单元格进行重新排序的自定义子类UICollectionViewFlowLayout
。源代码here。UICollectionView单元笔尖未加载
我遇到的问题是我为集合视图创建的nib文件无法加载,尽管注册它并使用了正确的标识符。以下是相关的代码片段。
这是在视图控制器.m
文件:
- (void)loadView
{
self.reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:self.reorderLayout];
UINib *nib = [UINib nibWithNibName:@"CatagoryViewCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"CatagoryViewCellID"];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Catagory *c = [[[CatagoryStore defaultStore] allCatagories]
objectAtIndex:[indexPath item]];
CatagoryViewCell *cell = (CatagoryViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CatagoryViewCellID" forIndexPath:indexPath];
[cell setController:self];
[[cell nameLabel] setText:c.title];
return cell;
}
这些是仅有的两个地方,我引用的笔尖。
这里是小区笔尖:
这就是它看起来像在模拟器和设备:
我尝试使用类似的代码和原始UICollectionViewFlowLayout
类,它的工作原理精细。任何有识之士都非常感谢!
答
尝试用你的CollectionViewCell.m这里面 “initWithFrame” 方法
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSString *cellNibName = @"YourCollectionViewCell";
NSArray *resultantNibs = [[NSBundle mainBundle] loadNibNamed:misVentasCellNibName owner:nil options:nil];
if ([resultantNibs count] < 1) {
return nil;
}
if (![[resultantNibs objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [resultantNibs objectAtIndex:0];
}
return self;
}
然后在课堂上你必须去集合视图中添加:
[collectionView registerClass:[YourCollectionViewCell class] forCellWithReuseIdentifier:@"YourCollectionViewCell"];
最后
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell identifier
static NSString *cellIdentifier = @"YourCollectionViewCell";
YourCollectionViewCell *cell = (YourCollectionViewCell *)[cvArticles dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}
请记住,您必须在your.xib中插入您在Co的标识符字段中使用的cellIdentifier选举视图!!!!!!!!
您使用ARC吗? – Jorge 2013-05-02 22:17:10
@Jorge是的,我 – isal 2013-05-02 22:35:24
看看这个帖子 http://stackoverflow.com/questions/14466959/create-uicollectionviewcell-subclass-with-xib – neaGaze 2013-10-08 07:32:28