在您的NSSplitViewController
-subclass viewDidLoad
-method 中以编程方式设置数据源。您还需要实现子视图控制器类(tableView
插座连接到控件)。
MySplitViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
for (NSSplitViewItem *item in self.splitViewItems)
{
NSViewController *controller = item.viewController;
if ([controller isKindOfClass:[MyChildController class]])
{
MyChildController *myController = (MyChildController *)controller;
myController.tableView.dataSource = self;
[myController.tableView reloadData];
}
}
}
但说实话,我不喜欢这种做法。当表格视图的数据源方法在本机视图控制器类中时会更好。
另一种方法来做到这一点。MyChildController.h 文件:
@class MyChildViewController;
@protocol MyChildControllerDelegate <NSObject>
- (void)childController:(MyChildViewController *)controller didSelectRowAtIndex:(NSUInteger)index;
@end
@interface MyChildViewController : NSViewController <NSTableViewDataSource, NSTableViewDelegate>
@property (nonatomic, weak) id<MyChildControllerDelegate> delegate;
@property (nonatomic, retain) NSArray *items;
@property (nonatomic, weak) IBOutlet NSTableView *tableView;
@end
不要忘记实现您需要的所有表格视图dataSource
和delegate
方法。MySplitViewController.m 文件:
- (void)viewDidLoad
{
[super viewDidLoad];
for (NSSplitViewItem *item in self.splitViewItems)
{
NSViewController *controller = item.viewController;
if ([controller isKindOfClass:[MyChildController class]])
{
MyChildController *myController = (MyChildController *)controller;
myController.delegate = self;
[myController setItems:_items];
}
}
}