使用Swift编程在代码中创建UITableViewController

7

我正在尝试以编程方式设置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。希望能得到帮助,非常感谢。

你是如何定义MainViewController类的? - drewag
MainViewController的定义是什么?你是直接从UITableViewController继承的吗?你定义了任何初始化器吗? - Lukas
@Lukas 我更新了代码。 - Nilzone-
3个回答

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


3

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


当我在MainViewController中删除了init函数时,程序运行得非常好。我刚刚在SO上看到了一个帖子,与我的问题相同(不知道为什么在发布之前没有看到那个帖子...)。但是这为什么是必要的呢? - Nilzone-
请确保在您自己的初始化程序中调用适当的继承init(...)方法。 - Ben Gottlieb
6
当你添加了一个指定初始化器时,Swift会使继承的初始化器对于派生类的客户端不可访问。这看起来类似于C++中的私有继承,其中继承的方法/数据被隐式移动到“private”类部分。原因很简单:如果有自定义初始化程序,则您的类可能需要此初始化程序来构造类“不变性”。 但是有一个bug:如果你创建了一个继承自UITableViewController的类,并添加了调用super.init(style:)的自定义初始化器,则会出现“_use of unimplemented initializer 'init(nibName:bundle:)'_”错误。 - Sergiy Salyuk

0

我只是在删除

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

然后像这样初始化:

 let mainViewController: UITableViewController = MainViewController()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接