如何在特定条件下在UITableView中显示消息

时间:2014-07-29 20:05:51

标签: ios objective-c uitableview

我在UITableView内有一个UITabBarController。现在,我已经在检查用户是否已登录,如果他们没有登录,我会显示一个模态视图控制器,让他们登录或注册。但是这是我的问题,当用户点击取消时,用户返回到表格视图的原始位置。如果用户没有登录,我无法让用户看到表视图中的行。如果用户没有登录,我想要一条消息,确切地知道Apple在iTunes商店中提供的示例以及“我附近的热门”示例Apple人机界面指南:

https://developer.apple.com/library/iOS/documentation/userexperience/conceptual/mobilehig/StartingStopping.html#//apple_ref/doc/uid/TP40006556-CH52-SW1

那么我如何在tableview控制器中显示这样的消息呢?请记住,我将始终拥有该表视图控制器的数据。希望我对每个人都很清楚。我是否只将tableview背景带到tableview行的前面?

// ATableViewController embedded in a NavigationController with UITabBar

- (void)viewWillAppear:(BOOL)animated
{
    // NSLog(@"dateViewDidLoad %f", [[NSDate date] timeIntervalSince1970]);
    [super viewWillAppear:animated];

    NSUserDefaults *textDef = [NSUserDefaults standardUserDefaults];
    NSString *userName = [textDef stringForKey:@"userName"];

    if (userName == nil) {
        SignUpViewController *signup = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
        [signup setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:signup animated:YES completion:nil];

        // Display message you need to sign in to view this content
    } else {
        // Proceed and display the rows
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用UITableViewController,您可能会滥用" tableView的tableHeaderView,通过将其调整为全屏并禁用dataSource方法并滚动。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (isLoggedIn) {
        self.tableView.tableHeaderView = nil;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.scrollEnabled = YES;
        [self.tableView reloadData];
    }
    else {
        if (!self.tableView.tableHeaderView) {
            UIView *viewToDisplayIfNotLoggedIn = [[UIView alloc] initWithFrame:self.view.bounds];
            viewToDisplayIfNotLoggedIn.backgroundColor = [UIColor redColor];
            self.tableView.tableHeaderView = viewToDisplayIfNotLoggedIn;
        }
        self.tableView.delegate = nil;
        self.tableView.dataSource = nil;
        self.tableView.scrollEnabled = NO;
        [self.tableView reloadData];
    }
}

如果您使用的是普通的UIViewController,那就更容易了,只需隐藏tableView并显示另一个UIView:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (isLoggedIn) {
        self.tableView.hidden = NO;
        self.viewToDisplayIfNotLoggedIn.hidden = YES;
    }
    else {
        self.tableView.hidden = YES;
        if (!self.viewToDisplayIfNotLoggedIn) {
            self.viewToDisplayIfNotLoggedIn = [[UIView alloc] initWithFrame:self.view.bounds];
            self.viewToDisplayIfNotLoggedIn.backgroundColor = [UIColor redColor];
            [self.view addSubview:self.viewToDisplayIfNotLoggedIn];
        }
        self.viewToDisplayIfNotLoggedIn.hidden = NO;
    }
}