UICollectionView Cell nib未加载

时间:2013-05-02 21:17:49

标签: iphone objective-c xcode uicollectionview uicollectionviewcell

我正在使用UICollectionViewFlowLayout的自定义子类,允许重新排序单元格。源代码here

我遇到的问题是我为Collection View创建的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;
}

这是我引用笔尖的唯一两个地方。

这是单元格笔尖: enter image description here

这就是模拟器和设备中的样子:

enter image description here

我尝试使用类似的代码和原始的UICollectionViewFlowLayout类,但它运行正常。非常感谢任何见解!

1 个答案:

答案 0 :(得分:6)

在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;    
}

然后在课程中你有de collection view add:

[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中插入您在Collection视图的Identifier字段中使用的相同cellIdentifier !!!!!!!!