【问题标题】:How can I display UILabel "No result found" in cell if no collectionview item?如果没有collectionview项目,如何在单元格中显示UILabel“未找到结果”?
【发布时间】: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


【解决方案1】:

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

- (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){

if (indexPath.item < _youLikeItem.count) {

(注意,对于集合视图,您应该使用 indexPath.item,而不是 indexPath.row。)

【讨论】:

  • 嗨,伙计,感谢您的及时行动。我遇到了这个错误“原因:'集合视图的数据源没有从 -collectionView:cellForItemAtIndexPath 返回有效单元格:索引路径 {length = 2, path = 3 - 0}'”有什么想法吗?
  • 你需要将gridcell = cell;移到if-else之外。
【解决方案2】:

你需要做的是

- (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;

}

【讨论】:

  • 嗨 Prajakta,我遇到了这个错误“原因:'集合视图的数据源没有从 -collectionView 返回有效单元格:cellForItemAtIndexPath:索引路径 {length = 2, path = 3 - 0}'" 有什么想法吗?
  • 如果您要返回网格单元,您需要将“无可用产品”的单元分配给该网格单元。在这种情况下 gridcell = cell;应该在 if-else 语句之外。基本上你需要从函数“返回有效(非零)单元格 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {"
【解决方案3】:

您可以使用DZNEmptyDataSet,它是一个第三方库,它添加了几个委托协议,您可以在其中返回要显示的数据(图像/标题/描述甚至自定义视图),如果您的表格视图或集合视图是空的,并且会自动显示它,确保它居中。

大量流行应用都在使用它。

你可以在这里找到它们。

DZNEmptyDataSet

EmptyDataSet-Swift

【讨论】:

    【解决方案4】:

    试试这个代码

    Objective-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
        }
    

    【讨论】:

    • 问题被标记为objective-c
    • 答案已修改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多