Add text when no entries in a table view

Send Snippet To: Save this snippet to Code Collector Pro -- view all balzan's snippets
language: Objective-C
license: Other

Code for Snippet:

in the .m file of your table view
@property (strong, nonatomic) UIView* zeroEntriesView;
@property (strong, nonatomic) UILabel* zeroEntriesLabel;
@property (nonatomic, strong) UIBarButtonItem* doneEditBarButton;
// in setupUI / or view did load if you re call setup UI
- (void)setupUI {
	self.zeroEntriesLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300, 30)];
	self.zeroEntriesLabel.text = @"No Groups have been saved";
	self.zeroEntriesLabel.backgroundColor = [UIColor clearColor];
	self.zeroEntriesView = [[UIView alloc] initWithFrame:self.tableView.frame];
	self.zeroEntriesView.backgroundColor = [UIColor clearColor];
	[self.zeroEntriesView addSubview:self.zeroEntriesLabel];
	UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
	footerView.backgroundColor = [UIColor clearColor];
	[self.tableView setTableFooterView:footerView];
	self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cardboard_flat.png"]];
	self.doneEditBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(editButtonTouched:)];
	self.groupArray = [AACoreDataManager loadObjectsWithName:@"Group" predicate:nil sortKey:@"name" sortAscending:YES];
	[self refreshUI];
- (void)refreshUI {
	if (self.groupArray.count == 0) {
		self.tableView.tableHeaderView = self.zeroEntriesView;
		self.tableView.tableHeaderView = nil;
	[self.tableView reloadData];
#pragma mark - Actions
//actions section
- (IBAction)editButtonTouched:(id)sender {
	if (self.tableView.isEditing) {
		[self.tableView setEditing:NO animated:YES];
		[self.navigationItem setLeftBarButtonItem:self.editBarButton animated:YES];
		[self.tableView setEditing:YES animated:YES];
		[self.navigationItem setLeftBarButtonItem:self.doneEditBarButton animated:YES];
comments powered by Disqus


Link to this snippet:

Download to Code Collector

To use the direct link to your snippet on either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 357
Date Added: 2012-09-26 06:51:39
Last Modified: 2014-02-18 20:29:30

Web Analytics