正如标题所说,我正在尝试以编程方式设置 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
任何帮助将非常感激