[英]iOS : Collection view doesn't show
I have a collection view in my app, I want it to contain a custom cells. 我的应用程序中有一个集合视图,我希望它包含一个自定义单元格。 I've created a custom cell view xib file.
我创建了一个自定义单元格视图xib文件。 Then I use it in my data source method :
然后在数据源方法中使用它:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath];
if(cell == nil){
NSArray *nsObjects = [[NSBundle mainBundle] loadNibNamed:@"OtherCustomersCell" owner:nil options:nil];
for(id obj in nsObjects)
if([obj isKindOfClass:[OtherCustomersCell class]])
cell = (OtherCustomersCell*) obj;
}
[cell.name setText:@"AAAA BBBBB"];
return cell;
}
But when I run the app, there is just a black rectangular where the collection view should be (at the bottom under the table view): 但是,当我运行该应用程序时,集合视图应位于表格视图下方的黑色矩形中:
What am I doing wrong? 我究竟做错了什么? Thank you in advance.
先感谢您。
Collection views work differently than table views in that you don't have to create a cell if one can't be dequeued. 集合视图的工作方式与表视图不同,因为如果不能使一个单元出队,则不必创建一个单元。
Instead, you have to register the nib for the cell first: 相反,您必须先为该单元注册笔尖:
- (void)viewDidLoad
{
...
UINib *cellNib = [UINib nibWithNibName:@"OtherCustomersCell" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER];
}
You can then dequeue the cell and it will be created automatically for you if necessary: 然后,您可以使单元出队,并在必要时自动为您创建:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath]; // cell won't be nil, it's created for you if necessary!
[cell.name setText:@"AAAA BBBBB"];
return cell;
}
您必须按照以下方式在viewdidload中注册UICollectionView
实例,然后才能使用它。
[self.photoListView registerNib:[UINib nibWithNibName:@"UIcollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Identifier"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.