开发者

Present a default view instead of tableview if datasource is empty

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 l开发者_运维问答ook and give me the proper way to do this kind of behavior ?

Thanks,


I solved this problem by adding a UIView in the tableview header with a frame size equal to tableview size and disallowing user interaction on the tableview:

    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 data you just remove the header and reenable user interaction:

    self.tableView.tableHeaderView = nil;
    self.tableView.userInteractionEnabled = YES;


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜