3

I am looking for 3 hours now on Google how to remove the tableview and show an image when the tableview is empty (have no more rows). Does someone know this? I know it's possible, because I saw it on many apps.

What I could find was:

// Check if table view has any cells
int sections = [self.tableView numberOfSections];
BOOL hasRows = NO;
for (int i = 0; i < sections; i++)
    hasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO;

if (sections == 0 || hasRows == NO)
{
    UIImage *image = [UIImage imageNamed:@"test.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // Add image view on top of table view
    [self.tableView addSubview:imageView];

    // Set the background view of the table view
     self.tableView.backgroundView = imageView;
}

where to put this?

Thanks!

user1883396
  • 161
  • 3
  • 9

4 Answers4

6

If your using Storyboard just put your view behind your UITableView

If your array of data is empty when creating it, simply hide your UITableView to show the "empty table" view behind it.

[tableView setHidden:YES];
RyanG
  • 4,393
  • 2
  • 39
  • 64
  • And I should put this in the `- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {` ? – user1883396 Jan 15 '13 at 19:35
  • Put this wherever you are creating your array your using for your table-- After you add objects to it and before you reload your table, check to see if that array's count is 0, if it is than just hide table then – RyanG Jan 15 '13 at 19:37
1

Please refer to: http://www.unknownerror.org/Problem/index/905493327/how-do-i-display-a-placeholder-image-when-my-uitableview-has-no-data-yet/

Thanks to Cocoanut and Thomas:

@interface MyTableViewController : UITableViewController
{
      BOOL hasAppeared;
      BOOL scrollWasEnabled;
      UIView *emptyOverlay;
}

- (void) reloadData;
- (void) checkEmpty;

@end

@implementation MyTableViewController

- (void)viewWillAppear:(BOOL)animated
{
   [self reloadData];
   [super viewWillAppear: animated];
}

- (void)viewDidAppear:(BOOL)animated
{
   hasAppeared = YES;

   [super viewDidAppear: animated];

   [self checkEmpty];
}

- (void)viewDidUnload
{
   if (emptyOverlay)
   {
      self.tableView.scrollEnabled = scrollWasEnabled;
      [emptyOverlay removeFromSuperview];
      emptyOverlay = nil;
   }
}

- (UIView *)makeEmptyOverlayView
{
    UIView *emptyView = [[[NSBundle mainBundle] loadNibNamed:@"myEmptyView" owner:self options:nil] objectAtIndex:0];
    return emptyView;
}

- (void) reloadData
{
   [self.tableView reloadData];

   if (hasAppeared &&
       [self respondsToSelector: @selector(makeEmptyOverlayView)])
      [self checkEmpty];
}

- (void) checkEmpty
{
   BOOL isEmpty = YES;

   id<UITableViewDataSource> src = self.tableView.dataSource;
   NSInteger sections(1);
   if ([src respondsToSelector: @selector(numberOfSectionsInTableView:)])
      sections = [src numberOfSectionsInTableView: self.tableView];
   for (int i = 0; i < sections; ++i)
   {
      NSInteger rows = [src tableView: self.tableView numberOfRowsInSection: i];
      if (rows)
         isEmpty = NO;
   }

   if (!isEmpty != !emptyOverlay)
   {
      if (isEmpty)
      {
         scrollWasEnabled = self.tableView.scrollEnabled;
         self.tableView.scrollEnabled = NO;
         emptyOverlay = [self makeEmptyOverlayView];
         [self.tableView addSubview: emptyOverlay];
      }
      else
      {
         self.tableView.scrollEnabled = scrollWasEnabled;
         [emptyOverlay removeFromSuperview];
         emptyOverlay = nil;
      }
   }
   else if (isEmpty)
   {
      // Make sure it is still above all siblings.
      [emptyOverlay removeFromSuperview];
      [self.tableView addSubview: emptyOverlay];
   }
}

@end
lmnbeyond
  • 6,383
  • 2
  • 18
  • 18
0

UITableView is a (non direct) subclass of UIView, so what you want to do is easy.

Say that you have this table view as subview of your view controller's view. This case you just create another view with the same frame as the table view, then you remove your table view from the superview, and add the newly created view as subview of your view controller's view. So simply:

UIImageView* view= [[UIImageView alloc] initWithImage: yourImage];
view.frame= tableView.frame;
[tableView removeFromSuperview];
[self.view addSubview: view];
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
-3

Put it on - (void)viewDidAppear. Good luck ;)