【问题标题】:Present a default view instead of tableview if datasource is empty如果数据源为空,则显示默认视图而不是表格视图
【发布时间】:2010-09-07 15:55:17
【问题描述】:

我目前正在开发的 iOS 应用是基于标签栏的,其中一个标签是 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。

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

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

谢谢,

【问题讨论】:

  • myDatas...'data' 是复数...只是说
  • 已修复!现在每个人都知道我不是以英语为母语的人 :p 谢谢 ;)

标签: iphone objective-c ios uitableview


【解决方案1】:

我通过在 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;

【讨论】:

  • 对您的答案的一个建议:因为emptyView 中的userInteractionEnabled = NO; 任何交互元素(即UIButton 等)都无法正常工作。你应该使用self.tableView.scrollEnabled = NO; ;)
【解决方案2】:

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

【讨论】:

  • 我怕有人告诉我!如果没有其他人提出其他想法,我可能会切换到常规 UIViewcController 作为最后的手段......谢谢!
  • 就个人而言,我创建了一个模仿 UITableViewController 的 UIViewController 子类,因为它允许您添加子视图。将更新答案。
【解决方案3】:

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

【讨论】:

  • 这是一个想法...我以前从未玩过标题视图:您可以设置它以便消息可以垂直居中显示吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 2013-05-27
  • 2012-04-29
相关资源
最近更新 更多