相关疑难解决方法(0)

使用xib创建UICollectionViewCell子类

我正在尝试创建一个UICollectionViewCell链接了一个xib 的子类,我已经这样做了:我创建了一个新的xib文件并UICollectionViewCell在其中添加了一个,然后我创建了这个子类文件:

@interface MyCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UILabel *label;
@end
Run Code Online (Sandbox Code Playgroud)

另外我在文件所有者自定义类中链接了MyCell接口构建器中的类,并且我添加了一个UILabel,然后在我的UICollectionViewviewDidLoad中我这样做:

[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];
Run Code Online (Sandbox Code Playgroud)

除此之外:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];


cell.label.text = @"Cell Text";


return cell;
}
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,我收到此错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the …
Run Code Online (Sandbox Code Playgroud)

iphone xib ios uicollectionview uicollectionviewcell

46
推荐指数
2
解决办法
6万
查看次数