[英]How to display message in UITableView under certain conditions
我在UITabBarController
里面有一个UITableView
。 现在,我已经在检查用户是否已登录,如果他们没有登录,我将显示一个Modal View Controller,让他们登录或注册。 但是这是我的问题,当用户单击“取消”时,用户将返回到表格视图的原始位置。 如果用户未登录,则无法让用户在表视图中看到行。如果用户未登录,我想要一条消息,确切地讲苹果在iTunes商店中提供的示例以及“我附近的热门”示例中的示例苹果人机界面指南:
那么,如何在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
}
}
如果使用UITableViewController,则可以通过将其调整为全屏大小并禁用dataSource方法并滚动来“滥用” tableView的tableHeaderView
。
- (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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.