Test 87
Test 87

Reputation: 111

How can I display UILabel "No result found" in cell if no collectionview item?

I would like to display UILabel "No result found" in cell ("HomeProductCell" as below") if no data in collectionview. How can I set for this label in cell? Please help.

Here is my code:-

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

        }
    }

Upvotes: 0

Views: 878

Answers (4)

EmilioPelaez
EmilioPelaez

Reputation: 19912

You can use DZNEmptyDataSet, it's a third party library that adds a couple of delegate protocols where you'll return the data (image/title/description or even a custom view) you want to display if your table view or collection view are empty, and will take care of showing it automatically, making sure it's centered.

It's used by a ton of popular apps.

You can find them here.

DZNEmptyDataSet

EmptyDataSet-Swift

Upvotes: 0

a.masri
a.masri

Reputation: 2469

try this code

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

Swift

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

}

Use extension

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

Upvotes: -1

Prajakta
Prajakta

Reputation: 322

What you need to do is

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

}

Upvotes: 1

Guy Kogus
Guy Kogus

Reputation: 7351

Make sure that you indicate that there's at least 1 item in section 3:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 3) { //Product
        if (_youLikeItem.count > 0) {
           return _youLikeItem.count;  
        } else {
           return 1;
        }
    }
    return 0;
}

That will do the trick. If you want to be extra careful then in collectionView:cellForItemAtIndexPath: you can change

if(_youLikeItem.count > 0){

to

if (indexPath.item < _youLikeItem.count) {

(N.B. For collection views you should use indexPath.item, not indexPath.row.)

Upvotes: 1

Related Questions