5

Was wondering how to properly display a notification or text when there are no rows / cells found on my UICollectionViewController and UITableViewController. At this time I would only show a UIAlertView, is there a better way to approach this?

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Warning!" 
message:@"No rows found" 
delegate:self
cancelButtonTitle:@"Logout"];

[alert show];
Plague
  • 466
  • 5
  • 15
gdubs
  • 2,724
  • 9
  • 55
  • 102

6 Answers6

13

UICollectionView (and UITableView) has a property named backgroundView. You can create a view with a label, and just play with the hidden property of it.

I actually use this technic to show the "no rows" label and the "loading" label.

tal952
  • 953
  • 12
  • 18
7

I personally use the UICollectionView footer to show this kind of messages. I make a footer in the Story Board(Section Footer selected in the attribute inspector of the story board) with a label with the message and then in the UICollectionViewController:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionFooter) {
        reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

        if(images.count>0){
            reusableview.hidden = YES;
            reusableview.frame = CGRectMake(0, 0, 0, 0);
        }else{
            reusableview.hidden = NO;
            reusableview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        }

    }

    return reusableview;
}
kahlo
  • 2,314
  • 3
  • 28
  • 37
  • I think I went with this but used the header instead. Would footer make more sense? – gdubs Oct 30 '13 at 17:56
  • I use footer because the header adds me extra space I am not able to get rid off. – kahlo Oct 30 '13 at 18:39
  • It may be worth stating (as I had this problem when I implemented your solution) that when you add the Section Footer to your `UICollectionView` from the Storyboard, you also need to give the footer an identifier. This is in the Attributes list under "Collection Reusable View". In @Mingot 's example the identifier is "FooterView". Really helpful solution! – GaryEmery Sep 17 '14 at 09:36
  • there is an issue for this approach that becasue you need to set the footer height in - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section , so when there is a record, though you hide the footer, footer's space is still there, which will leave blank space at the bottom when scrolling. – Hammer Jan 09 '15 at 16:02
3

As mentioned by @tal952, you can use backgroundView property of UICollectionView or UITableView if entire collectionView/tableView is empty.

Here is what I use, the same can be applied to tableView as well:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if myArray.isEmpty && collectionView.backgroundView == nil {
        let noItemLabel = UILabel() //no need to set frame.
        noItemLabel.textAlignment = .center
        noItemLabel.textColor = .lightGray
        noItemLabel.text = "No item present"
        collectionView.backgroundView = noItemLabel
    }

    collectionView.backgroundView?.isHidden = !myArray.isEmpty
    return myArray.count
}

If you have footers or headers present, set their isHidden property accordingly:

footer.isHidden = self.myArray.isEmpty

empty collectionView/tableView

Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60
1

Use this Tweet box info like alert. its catchy Tweet like info Panel

iAhmed
  • 6,556
  • 2
  • 25
  • 31
  • 1
    this looks cool, but I would like to have some kind of permanent text on there like what @brhane was recommending. i am sure i'd end up using this as well. thanks btw! – gdubs Oct 02 '13 at 20:03
0

Create a UIView that has a label that says "No rows found" (_noRowsView). Then use that as a tableHeaderView when the number of rows is 0.

-(void)dataLoadingDidFinish {
   if(_data.count == 0) {
       self.tableView.tableHeaderView = _noRowsView;
   } else {
      self.tableView.tableHeaderView = nil;
   }

}
0

The best answer is in How to change the UICollectionView footerview's height programatically

change the reusableview.frame in viewForSupplementaryElementOfKind is never working. It is misleading to mark the kahlo's reply as the correct answer.

Community
  • 1
  • 1
Hammer
  • 8,538
  • 12
  • 44
  • 75