我正在尝试使用设置了 UITabBar 和 UITableViews 的 GUI。
我有一个以编程方式创建的 UITabView。 其中一个选项卡显示一个 UITableView,它也是以编程方式创建的。
此 UITableView 然后在调用 didSelectRowAtIndexPath 时显示其他 View 。
不幸的是,单击表格单元格时,我的选项卡 View 消失并显示新的表格 View 。
我无法理解的是如何构建 View 以使 tabBar 保留在屏幕上。
它是像缩短 UITableView 一样简单,还是我缺少一些窗口/ View 的魔力?
谢谢
最佳答案
您应该使用 UITabBarController 来显示 UITabBar 而不是直接这样做。
然后使用 UITableViewController 作为给定选项卡的 View Controller 。虽然我得到的印象是你想在选择一行时显示后代 UITableViews。如果是这种情况,您应该使用 UINavigationController 作为标签栏的 View Controller ,并让它管理您的 UITableViewController。
请记住,在 iOS 上您确实需要使用 View Controller 模式 - 框架会在幕后为您处理很多事情。
跟进:
好的,下面的简单实现对我来说效果很好。请忽略此代码的许多明显问题(首先是我在应用程序委托(delegate)中将其全部破解!);它的目的纯粹是作为您的 Controller 应该如何粘合在一起的模型。
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;
@end
@implementation AppDelegate
@synthesize window = _window;
@synthesize navController = _navController;
- (void)dealloc
{
[_navController release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UITableViewController *rootTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[[rootTVC tableView] setDelegate:self];
[[rootTVC tableView] setDataSource:self];
[rootTVC setTitle:@"Root Table"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootTVC];
[rootTVC release];
[navController setTitle:@"My Table"];
UIViewController *anotherViewController = [[UIViewController alloc] init];
[anotherViewController setTitle:@"Not the table"];
UITabBarController *tbc = [[UITabBarController alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:navController, anotherViewController, nil]];
[self setNavController:navController];
[navController release];
[anotherViewController release];
[[self window] setRootViewController:tbc];
[tbc release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = @"foo";
UITableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:reuseIdentifier] retain];
if (! cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
[[cell textLabel] setText:[NSString stringWithFormat:@"Row %d", [indexPath row]]];
return [cell autorelease];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewController *newTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[newTVC setTitle:[NSString stringWithFormat:@"Table %d", [indexPath row]]];
[[newTVC tableView] setDelegate:self];
[[newTVC tableView] setDataSource:self];
[[self navController] pushViewController:newTVC animated:YES];
}
@end
关于ios - UITabBars 和 UITableViews。单击表格单元格时将选项卡栏保持在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10036844/