views:

38

answers:

2

Hi everyone,

The iOS app I'm currently working on is tabbar-based, and one of the tab is a UITableViewController.

The thing is, when I open this tab with an empty datasource (for whatever reason), I'd like to bring another view, with some kind of message/image, instead of the blank view I get with the tableviewcontroller.

I tried something like that :

- (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 emptyView defined as an iVar in the view controller.

But It doesn't work as expected, and I can't find the reason :/

Could any of you give it a look and give me the proper way to do this kind of behavior ?

Thanks,

A: 

UITableViewController doesn't allow you to add subviews to it's view (the tableView).

You should make a UIViewController and add the UITableView yourself with your optional emptyView.

Don't forget to set the dataSource and the delegate!

Update : I've made a subclass of UIViewController to avoid mimics UITableViewController every 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
gcamp
I was afraid someone would told me that !I might switch to a regular UIViewcController as a last resort if nobody else comes up with another idea...Thanks !
squizz
Personally, I made a subclass of UIViewController that mimics UITableViewController exempt for the fact that it let you add subviews. Will update the answer.
gcamp
Nice work, thanks for sharing !
squizz
A: 

You could perhaps try setting the number of rows to zero. Then inserting the no results view as the header view.

qui
That's an idea...I've never toyed with Header Views before : can you set it up so the message can be displayed vertically centered ?Thanks !
squizz