【发布时间】:2011-06-28 01:11:34
【问题描述】:
我很好奇是否有人有从 TableView 管理多个 ViewController 的想法。我在 TableView 中显示了大约七个项目的列表,每个项目都有一个专用的 ViewController。我的第一个想法是用各种 ViewController 初始化一个数组。
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:7];
[viewControllers addObject:[[ViewController1 alloc] initWithNibName:@"View1" bundle:nil]];
[viewControllers addObject:[[ViewController2 alloc] initWithNibName:@"View2" bundle:nil]];
...
然后引用该数组以加载有关项目选择的适当视图。
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.navigationController pushViewController:[viewControllers objectAtIndex:indexPath.row] animated:YES];
}
我真的不确定这是否合适。任何方向都会很棒。
已编辑:
根据 Ryan 和 Joe 的反馈,我实现了一个对象来保存我的表格项目。缩写我的问题也引起了对实现细节的一些混淆。添加了管理视图控制器和选择标签栏项目的完整解决方案。
TableNavigationItem.h
#import
@interface TableNavigationItem : NSObject {
NSString *title;
NSNumber *tabIndex;
id viewController;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSNumber *tabIndex;
@property (nonatomic, retain) id viewController;
@end
TableNavigationItem.m
#import "TableNavigationItem.h"
@implementation TableNavigationItem
@synthesize title;
@synthesize viewController;
- (id) init{
if(self = [super init]){
self.title = @"";
}
return self;
}
- (void) dealloc {
[title release];
[tabIndex release];
[viewController release];
[super dealloc];
}
@end
然后按照 Joe 的建议进行初始化。
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:7];
TableNavigationItem *navItem;
// view 1
navItem = [[TableNavigationItem alloc] init];
navItem.title = @"View 1";
navItem.tabIndex = [NSNumber numberWithInt:1];
[mutableArray addObject:navItem];
[navItem release];
// view 2
navItem = [[TableNavigationItem alloc] init];
navItem.title = @"View 2";
navItem.viewController = [ViewController2 class]];
[mutableArray addObject:navItem];
[navItem release];
...
// store the navigation items
self.tableItems = [NSArray arrayWithArray:mutableArray];
[mutableArray release];
然后
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TableNavigationItem *navItem = [tableItems objectAtIndex:indexPath.row];
if(navItem.viewController != nil){
[self.navigationController pushViewController:[[[navItem.viewController alloc] init] autorelease] animated:YES];
}
else if(navItem.tabIndex != nil){
[((MyAppDelegate *)[UIApplication sharedApplication].delegate).tabBarController setSelectedIndex:[navItem.tabIndex integerValue]];
}
}
【问题讨论】:
-
我不确定我是否理解 TableNavigationItem 的意义。为什么不在
ViewController1和ViewController2上创建title方法?那么你的数组可以是:viewControllers=[NSArray arrayWithObjects:[ViewController1 class], [ViewController2 class], nil]而你的tableView:didSelectRowAtIndexPath:可以简单地调用[self.navigationController pushViewController:[[[[viewControllers objectAtIndex:indexPath.row] alloc] init] autorelease] animated:YES]。 -
另外,如果
tableItems是保留属性,则copy调用将导致泄漏,除非您自动释放该结果数组。但是你总是可以只分配给 NSMutableArray(谁会知道区别?),让它成为[NSMutableArray arrayWithCapacity:7]或者更好的是-[NSMutableArray arrayWithObjects:..., nil]!它还可以帮助您了解 Cocoa 的内存管理和对象生命周期。 -
+1 如前所述,TableNavigationItem 有点多余。我删除了一些用例以简化问题(我有一些导航项可以导航标签栏控制器)。我想我应该提出整个问题。
-
+1 你是对的。我没有意识到副本增加了保留计数(我很少使用它)。
标签: iphone uitableview uiviewcontroller