【发布时间】:2018-04-16 14:09:27
【问题描述】:
如果collectionview中没有数据,我想在单元格中显示UILabel“未找到结果”(“HomeProductCell”如下“)。 如何在单元格中设置此标签?请帮忙。
这是我的代码:-
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 3) { //Product
if(_youLikeItem.count >0)
{
return _youLikeItem.count;
}
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *gridcell = nil;
if (indexPath.section == 3) {
HomeProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeProductCellID forIndexPath:indexPath];
if(_youLikeItem.count > 0){
cell.productImage = [_youLikeItem[indexPath.row]valueForKey:@"image"];
cell.productName = [_youLikeItem[indexPath.row]valueForKey:@"name"];
cell.productPrice = [_youLikeItem[indexPath.row]valueForKey:@"price"];
gridcell = cell;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.bounds.size.width, collectionView.bounds.size.height)];
noDataLabel.text = @"No product(s) available";
noDataLabel.textColor = [UIColor grayColor];
noDataLabel.font = PFR18Font;
noDataLabel.textAlignment = NSTextAlignmentCenter;
[cell addSubview:noDataLabel];
}
}
【问题讨论】:
-
如果你看到你的 else 部分永远不会被执行,但是这样做是错误的。在故事板中以隐藏模式创建标签,现在当您获取数据时,即 _youLikeItem 检查计数是否为 0,然后取消隐藏标签并隐藏集合视图
-
正确,不要将标签放在单元格中,而是将其覆盖在视图上。
标签: ios objective-c collectionview