在Swift中以编程方式创建一个UITableViewController [英] create a UITableViewController programmatically in Swift

查看:354
本文介绍了在Swift中以编程方式创建一个UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我试图以编程方式设置UITableViewController。经过几个小时的尝试,我希望有人可以帮助我。并且,是的,我已经检查过此事的其他帖子:

I'm trying to, as the title say, set up a UITableViewController programmatically. After a few hours of trying I hope someone can help me. And, yes, I hve checked out other posts on this matter:

import UIKit

class MainViewController: UITableViewController {

    init(style: UITableViewStyle) {
        super.init(style: style)
        // Custom initialization
    }

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // #pragma mark - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return 5
    }


    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
        var cell = tableView?.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell

        if !cell {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
        }
        cell!.textLabel.text = "test"
        return cell
    }

}

和appDelegate看起来像这样:

and the appDelegate looks like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

        let mainViewController: UITableViewController = MainViewController(style: UITableViewStyle.Plain)
        let navigationController: UINavigationController = UINavigationController()
        navigationController.pushViewController(mainViewController, animated: false)

        self.window!.rootViewController = navigationController
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        return true
    }

程序运行,但是一旦运行,我就会收到以下错误:

The program run, but as soon as it does, I get the following error:

fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'HelloWorld.MainViewController'

然后我更改了 MainViewController(样式:UITableViewStyle.Plain) MainViewController(nibName:n il,bundle:nil)但是我得到以下语法错误:调用中的额外参数'bundle'

I then changes the MainViewController(style: UITableViewStyle.Plain) to MainViewController(nibName: nil, bundle: nil) but then I get the following syntax error: Extra argument 'bundle' in call

非常感谢任何帮助

推荐答案

我正在使用UITableViewController的子类而没有任何问题,使用(nibName:bundle :)表单,但我在我的子类中重写了它。我尝试用标准的UITableViewController替换我的子类,它仍然工作正常。您是否可能覆盖子类中的init(...)方法?

I'm using a subclass of UITableViewController with no issues, using the (nibName:bundle:) form, but I've overridden that in my subclass. I tried replacing my subclass with a standard UITableViewController, and it still worked fine. Are you possibly overriding an init(...) method in your subclass?

这篇关于在Swift中以编程方式创建一个UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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