如果数据源为空,显示默认视图而不是表格视图

4

我目前正在开发的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。

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

你们能否看一下,并给我正确的方法来实现这种行为呢?

谢谢!


我的数据...“数据”是复数形式...只是说一下 - Felixs
修复了!现在大家都知道我不是母语英语者了 :p 谢谢 ;) - squizz
3个回答

4

我通过在tableview的标题中添加一个frame大小等于tableview大小并且禁止用户与tableview交互的UIView来解决了这个问题:

    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;

你的回答有一个建议:由于 "userInteractionEnabled = NO;",在 "emptyView" 中的任何交互元素(例如 "UIButton" 等)都无法正常工作。你应该使用 "self.tableView.scrollEnabled = NO;" ;) - damirstuhec

1

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

您应该创建一个 UIViewController,并使用可选的 emptyView 添加 UITableView

不要忘记设置 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

我担心有人会告诉我这个!如果没有其他的想法,作为最后的选择,我可能会切换到普通的 UIViewcController...谢谢! - squizz
个人而言,我创建了一个UIViewController的子类,它模仿了UITableViewController,除了它允许您添加子视图。我会更新答案。 - gcamp

0

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


这是个好主意... 我以前从没玩过Header Views:你能设置一下让消息垂直居中显示吗? 谢谢! - squizz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接