如果没有collectionview项目,如何在单元格中显示UILabel“找不到结果”?

时间:2018-04-16 14:09:28

标签: ios objective-c collectionview

如果在集合视图中没有数据,我想在单元格中显示UILabel“No result found”(“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];

        }
    }

4 个答案:

答案 0 :(得分:1)

确保您在第3部分中指出至少有一项:

az login

这样就可以了。如果你想要格外小心,那么在- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 3) { //Product if (_youLikeItem.count > 0) { return _youLikeItem.count; } else { return 1; } } return 0; } 你可以改变

collectionView:cellForItemAtIndexPath:

if(_youLikeItem.count > 0){

(N.B。对于集合视图,您应该使用if (indexPath.item < _youLikeItem.count) {,而不是indexPath.item。)

答案 1 :(得分:1)

您需要做的是

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

if (section == 3) { //Product

    if(_youLikeItem.count >0)
    {
       return _youLikeItem.count;  
    }else{
       return 1; //this will ensure that there will be single cell in this section which will display 'No product(s) available'
    }
}
return 0;

}

答案 2 :(得分:0)

您可以使用DZNEmptyDataSet,它是第三方库,可添加一些代理协议,您可以在其中返回您想要的数据(图像/标题/说明甚至是自定义视图)显示表格视图或集合视图是否为空,并自动显示它,确保它居中。

它被大量热门应用所使用。

你可以在这里找到它们。

DZNEmptyDataSet

EmptyDataSet-Swift

答案 3 :(得分:-1)

试试这段代码

<强>目标c

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
 if ([dataSource count] > 0)
{

    collectionView.backgroundView = nil;
}
else
{   
    UILabel *noDataLabel         = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.bounds.size.width, collectionView.bounds.size.height)];
    noDataLabel.text             = @"No data found :(";
    noDataLabel.textColor        = [UIColor blackColor];
    noDataLabel.textAlignment    = NSTextAlignmentCenter;
    collectionView.backgroundView = noDataLabel;
    collectionView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

return dataSource;
}

<强>夫特

extension UICollectionView{

    func noDataFound(_ dataCount:Int){
        if dataCount <=  0 {

            let label = UILabel()
            label.frame = self.frame
            label.frame.origin.x = 0
            label.frame.origin.y = 0 
            label.textAlignment = .center
            label.text = "No data found :("
            self.backgroundView = label
        }else{
            self.backgroundView = nil
        }
    }

}

使用扩展程序

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        collectionView.noDataFound(dataSource.count)
        return dataSource.count
    }
相关问题