2015-09-06 88 views
1

为什么我的子类不显示单元格?子类UICollectionViewCell不显示

我已经打过电话清醒从笔尖initWithCoderinitWithFrame但没有任何属性都显示

@interface PhotoCollectionViewCell : UICollectionViewCell 
@property (strong, nonatomic) IBOutlet UIImageView *photoImageView; 
@end 

,并在InterfaceBuilder中我.m文件

@implementation PhotoCollectionViewCell 
-(void)awakeFromNib { 
    self.photoImageView.frame = self.bounds; 
    self.backgroundColor = [UIColor lightGrayColor]; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
    self.photoImageView.frame = self.bounds; 
    self.backgroundColor = [UIColor lightGrayColor]; 
    } 
    return self; 
} 

我的细胞的类设置为PhotoCollectionViewCell,并且在接口构建器中正确设置了重用标识符

@interface HomeViewController() { 
    NSMutableArray *photosArray; 
} 
@end 

而且homeview.m文件

-(void)viewDidLoad { 
    [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"photoCell"]; 
    photosArray = [[NSMutableArray alloc] init]; 
    UIImage *placeholder = [UIImage imageNamed:@"placeholder.png"]; 
    [photosArray addObject:placeholder]; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return photosArray.count; 
} 

- (PhotoCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath]; 
    cell.photoImageView.image = [photosArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

des

dfa

df

+0

你不需要注册类或笔尖。这就是你在故事板中所做的。通过注册课程,您将覆盖故事板,并且您将获得您的手机,但没有连接任何插座。删除行'registerClass ...' – Fogmeister

回答

1

您不需要注册类或笔尖。这就是你在故事板中所做的。通过注册课程,您将覆盖故事板,并且您将获得您的手机,但没有连接任何插座。

删除行...

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

,因为你在故事板做你不需要它。

+0

美丽!谢谢。我其实已经阅读了关于它的苹果文档,并没有在那里注释。我会花几个小时甚至没有尝试。不幸的是,我看过的所有教程都在他们的教程中,即使他们使用故事板。我猜想信任教程的失败。谢谢 –

-1

在homeview.m要注册的NIB不是类。

UINib *cellnib = [UINib nibWithNibName:PhotoCollectionViewCell bundle:nil]; 
[self.collectionView registerNib:cellnib forCellReuseIdentifier:@"photoCell"]; 
+0

我不使用笔尖,它不是一个表查看 –