【发布时间】:2014-06-03 19:42:46
【问题描述】:
我正在尝试使用 swift 以编程方式创建一个简单的 tableView,所以我在“AppDelegate.swift”上编写了这段代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var tvc :TableViewController = TableViewController(style: UITableViewStyle.Plain)
self.window!.rootViewController = tvc
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
基本上我添加了 TableViewController 创建并添加到窗口中。这是 TableViewController 代码:
class TableViewController: UITableViewController {
init(style: UITableViewStyle) {
super.init(style: style)
}
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 10
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = "Hello World"
return cell
}
}
所以,当我尝试运行代码时,我会收到以下消息:
Xcode6Projects/TableSwift/TableSwift/TableViewController.swift: 12: 12: 致命错误:对类“TableSwift.TableViewController”使用未实现的初始化程序“init(nibName:bundle:)”
编译器在执行时发生错误
super.init(style: style)
有什么想法吗?
【问题讨论】:
-
尝试在你的类中添加/覆盖 init(nibName:bundle:) 方法。
标签: ios uitableview swift