Seb*_*ian 22 uitableview ios swift
我正在尝试使用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
}
Run Code Online (Sandbox Code Playgroud)
基本上我添加了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
}
Run Code Online (Sandbox Code Playgroud)
}
所以,当我尝试运行代码时,我收到此消息:
Xcode6Projects/TableSwift/TableSwift/TableViewController.swift:12:12:致命错误:对类'TableSwift.TableViewController'使用未实现的初始化程序'init(nibName:bundle :)'
编译器执行时发生错误
super.init(风格:风格)
有什么想法吗 ?
Sul*_*han 15
在Xcode 6 Beta 4中
删除
init(style: UITableViewStyle) {
super.init(style: style)
}
Run Code Online (Sandbox Code Playgroud)
会做的.这是由Obj-C和Swift之间的不同初始化器行为引起的.您已创建指定的初始化程序.如果删除它,将继承所有初始值设定项.
根本原因可能-[UITableViewController initWithStyle:]
就是召唤
[self initWithNibName:bundle:]
Run Code Online (Sandbox Code Playgroud)
我实际上认为这可能是Obj-C类转换为Swift类的方式中的一个错误.
ma1*_*w28 10
代替
init(style: UITableViewStyle) {
super.init(style: style)
}
Run Code Online (Sandbox Code Playgroud)
你可能会发现这个方便:
convenience init() {
self.init(style: .Plain)
title = "Plain Table"
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以调用TableViewController()
初始化.