Ada*_*dam 13 iphone cocoa-touch uikit ios
我正在将一个UIViewController加载到我的一个Nav控制器层次结构中,它将包含一些文本和一些图像.在底部,我将要创建一个可扩展和可折叠的tableview.
首先,这个想法有可能吗?如果是,我该如何添加它以及在何处放置数据源和委托方法?
我可以只创建一个TableViewController的子类,然后将其作为子视图添加到我的ViewController中吗?
com*_*nda 20
是的,您可以创建一个UITableView,其委托,数据源和父视图不一定是UITableViewController.由于UITableView是UIView,您可以将其添加为任何其他UIView的子视图.只要您实现所需的协议方法,任何NSObject都可以是委托或数据源.
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Run Code Online (Sandbox Code Playgroud)
事实上,根据我的经验,甚至没有多少人使用UITableViewControllers.您最后一次希望表视图占用整个可用空间是什么时候?通常,我创建一个普通的旧UIViewController,并添加一个UITableView作为其视图的子视图,以及其他子视图.
3lv*_*vis 15
/************************************************/
/************* MyCustomController.m *************/
/************************************************/
@interface MyCustomController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation MyCustomController
- (id)initWithNibName:(NSString*)nibName bundle:(NSString*)bundleName
{
self = [super initWitNibName:nibName bundle:bundleName];
if (self)
{
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.datasource = self;
tableView.delegate = self;
[self.view addSubview:self.tableView];
}
return self;
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return number of rows
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return cell
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// handle table view selection
}
@end
Run Code Online (Sandbox Code Playgroud)
Sha*_*ell 11
这很简单,就像你的viewDidLoad方法:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];
Run Code Online (Sandbox Code Playgroud)