如何以编程方式将ViewController转换为UITableViewController
我目前有一个MatchCenterViewController
,我想以编程方式转变为UITableViewController。 根据我发现的教程,我试图在下面这样做,但它似乎没有出现。
MatchCenterViewController.m:
#import "MatchCenterViewController.h" #import @interface MatchCenterViewController () @end @implementation MatchCenterViewController - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"newFriendCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newFriendCell"]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } //etc. return cell; } - (void)viewDidLoad { [super viewDidLoad]; } @end
至少,您需要实现以下方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
您需要设置委托和数据源,通常在viewDidLoad
- (void)viewDidLoad { [super viewDidLoad]; self.tableView.dataSource = self; self.tableView.delegate = self; }
此外,如果表视图是在storyboard中创建的,则需要IBOutlet
到表视图,如果表视图是在代码中创建的,则需要表视图的属性。