在Swift中以编程方式创建一个UITableViewController

时间:2014-06-07 15:34:05

标签: uitableview swift ios8 programmatically-created

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

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看起来像这样:

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
    }

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

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

然后我将MainViewController(style: UITableViewStyle.Plain)更改为MainViewController(nibName: nil, bundle: nil),但后来出现以下语法错误:Extra argument 'bundle' in call

非常感谢任何帮助

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

在AppDelegate中或在您调用TableViewController的任何地方:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    if let window = window {
      window.backgroundColor = UIColor.whiteColor()
      var mainTableViewController = MainTableTableViewController(style: .Plain)
      window.rootViewController = UINavigationController(rootViewController: mainTableViewController)
      window.makeKeyAndVisible()
    }
    return true
}

在UITableViewController中(假设没有自定义的TableViewCell实现):

import UIKit

class MainTableTableViewController: UITableViewController {

  override init(style: UITableViewStyle){
    super.init(style: style)
  }

  override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!){
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }

  required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

  }

  // MARK: - Tableview Data Source

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return [*YOUR_DATA_ARRAY].count
  }

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

    cell.textLabel!.text = [*YOUR_DATA_ARRAY][indexPath.row]
  }

完整示例:访问:https://github.com/ericcgu/EGStormTracker

答案 2 :(得分:0)

我只是删除

init(style: UITableViewStyle) {
    super.init(style: style)
}

然后像这样初始化:

 let mainViewController: UITableViewController = MainViewController()
相关问题