Present default view instead of tableview if data source is empty

The iOS application I'm working on is based on a time sheet, and one of the tabs is the UITableViewController.

The thing is, when I open this tab with an empty data source (for some reason), I would like to add another view with some kind of message / image instead of the empty view that I get with tableviewcontroller.

I tried something like this:

- (void)viewWillAppear:(BOOL)animated { if ([myData count] == 0) { if (!emptyView) { emptyView = [[UIView alloc] initWithFrame:self.view.frame]; UILabel *emptyMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, 20)]; emptyMsg.text = @"This is Empty !"; emptyMsg.textAlignment = UITextAlignmentCenter; [emptyView addSubview:emptyMsg]; } [self.view insertSubview:emptyView atIndex:0]; } else { if (emptyView != nil) { [emptyView removeFromSuperview]; emptyView = nil; } [self.tableView reloadData]; [super viewWillAppear:animated]; } } 

With an empty view defined as iVar in the view controller.

But it does not work as expected, and I can not find the reason: /

Can any of you look at him and give me the right way to commit this behavior?

Thanks,

+4
source share
3 answers

UITableViewController does not allow you to add views (tableView) to it.

You have to make a UIViewController and add a UITableView yourself using the optional emptyView .

Remember to set dataSource and delegate !

Update: I created a subclass of UIViewController to avoid imitating the UITableViewController each time.

.h

 // // GCTableViewController.h // GCLibrary // // Created by Guillaume Campagna on 10-06-17. // Copyright 2010 LittleKiwi. All rights reserved. // #import <UIKit/UIKit.h> //Subclass of UIViewController that mimicks the UITableViewController except that the tableView is a subview of self.view and allow change of the frame of the tableView @interface GCTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, readonly) UITableView *tableView; - (id) initWithStyle:(UITableViewStyle)style; //Subclass if you want to change the type of tableView. The tableView will be automatically placed later - (UITableView*) tableViewWithStyle:(UITableViewStyle) style; @end 

.m

 // // GCTableViewController.m // GCLibrary // // Created by Guillaume Campagna on 10-06-17. // Copyright 2010 LittleKiwi. All rights reserved. // #import "GCTableViewController.h" @implementation GCTableViewController @synthesize tableView; - (id) initWithStyle:(UITableViewStyle) style { if (self = [super initWithNibName:nil bundle:nil]) { tableView = [[self tableViewWithStyle:style] retain]; self.tableView.delegate = self; self.tableView.dataSource = self; } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; self.tableView.frame = self.view.bounds; self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); } - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; } #pragma mark TableView methods - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { return 0; } - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; } #pragma mark Getter - (UITableView *) tableViewWithStyle:(UITableViewStyle)style { return [[[UITableView alloc] initWithFrame:CGRectZero style:style] autorelease]; } - (void)dealloc { [tableView release]; tableView = nil; [super dealloc]; } @end 
+1
source

I solved this problem by adding a UIView to the tableview header with a frame size equal to the size of the tableview and prohibiting user interaction with the table view:

  UIView *emptyView = [[UIView alloc] initWithFrame:self.tableView.frame]; /* Customize your view here or load it from a NIB */ self.tableView.tableHeaderView = emptyView; self.tableView.userInteractionEnabled = NO; 

When you have the data, you simply delete the header and re-enable user interaction:

  self.tableView.tableHeaderView = nil; self.tableView.userInteractionEnabled = YES; 
+4
source

Perhaps you can try setting the number of rows to zero. Then insert the presentation of the results not as a heading.

0
source

All Articles