如果datasource为空,则显示默认视图而不是tableview

时间:2010-09-07 15:55:17

标签: iphone objective-c ios uitableview

我目前正在处理的iOS应用程序是基于tabbar的,其中一个选项卡是UITableViewController。

问题是,当我用空数据源(无论出于何种原因)打开此选项卡时,我想带另一个视图,带有某种消息/图像,而不是我用tableviewcontroller获取的空白视图。

我尝试过类似的东西:

- (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];
    }
}

在视图控制器中将 emptyView 定义为iVar。

但它没有按预期工作,我找不到原因:/

你们中的任何人都可以看看并给我正确的方法来做这种行为吗?

谢谢,

3 个答案:

答案 0 :(得分:4)

我通过在tableview标题中添加一个UIView来解决这个问题,其框架大小等于tableview大小,并禁止在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;

如果您有数据,只需删除标题并重新启用用户互动:

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

答案 1 :(得分:1)

UITableViewController不允许您将子视图添加到其视图(tableView)。

您应该制作一个UIViewController并使用可选的UITableView自行添加emptyView

不要忘记设置dataSourcedelegate

更新:我已经创建了UIViewController的子类,以避免每次都模仿UITableViewController。

.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

答案 2 :(得分:0)

您也许可以尝试将行数设置为零。然后插入无结果视图作为标题视图。