如何以编程方式将ViewController转换为UITableViewController [英] How to programmatically turn ViewController into a UITableViewController

查看:82
本文介绍了如何以编程方式将ViewController转换为UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 MatchCenterViewController ,我想以编程方式转变为UITableViewController。根据我发现的教程,我试图在下面这样做,但它似乎没有出现。

I currently have a MatchCenterViewController that I want to programmatically turn into a UITableViewController. I've attempted to do so below based on tutorials I've found, but it doesn't seem to be appearing.

MatchCenterViewController.m:

MatchCenterViewController.m:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@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


推荐答案

至少,你需要实现以下方法

As a minimum, you need to implement the following methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

您需要设置委托和数据源,通常在 viewDidLoad

And you need to set the delegate and datasource, typically in viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

此外,您需要 IBOutlet 到表视图,如果表视图是在storyboard中创建的,或者是表视图的属性,如果表视图是在代码中创建的。

Also, you need an IBOutlet to the table view if the table view was created in storyboard, or a property for the table view, if the table view was created in code.

这篇关于如何以编程方式将ViewController转换为UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆