使用Swift编写UITableView的程序化方法

22

我想用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: fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'TableSwift.TableViewController'

编译器执行

super.init(style: style)

时出现该错误。您有什么想法吗?


尝试在你的类中添加/覆盖init(nibName:bundle:)方法。 - 7c9d6b001a87e497d6b96fbd4c6fdf
2
请查看此链接:https://dev59.com/J2Ag5IYBdhLWcg3wDHQ3 - Pratik Bhiyani
4个回答

14

Xcode 6 Beta 4中

移除

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

将解决问题。这是由于Obj-C和Swift之间不同的初始化器行为引起的。您已创建了指定的初始化器。如果删除它,则会继承所有初始化器。

根本原因可能在于-[UITableViewController initWithStyle:]中调用的方法

[self initWithNibName:bundle:]

我实际上认为这可能是将Obj-C类转换为Swift类的方式中存在的一个错误。


我不得不调整cellForRowAtIndexPath中的其他内容,但现在它可以工作了。谢谢Sulthan! - Sebastian
你做了什么调整?我猜你应该使用 'tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")',因为你没有使用 xib 来创建单元格,是吗? - tafh
1
这个问题在Swift 2中最终得到解决,从XCode 7.0 beta (7A120f)开始可以直接使用super.init(style:style)... - Aymeric Barthe

10

取代

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

你可能会觉得这很方便:

convenience init() {
    self.init(style: .Plain)
    title = "Plain Table"
}

然后,您只需调用 TableViewController() 进行初始化。


在Xcode 6 beta 5中,您不能再声明一个无参数的便利初始化器。请参见:https://dev59.com/GV8e5IYBdhLWcg3w_-r0 - Nick Snyder
已经寻找了一个小时了。非常感谢!!适用于Xcode 7.0和Swift 2.0。 - Kevin van Mierlo

3
这很简单,就像编写一个函数一样。
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

    cell.text = self.Myarray[indexPath.row]
    cell.textLabel.textColor = UIColor.greenColor()

    cell.detailTextLabel.text = "DummyData #\(indexPath.row)"
    cell.detailTextLabel.textColor = UIColor.redColor()
    cell.imageView.image = UIImage(named:"123.png")
    return cell
}

cell.text已被弃用且不可用:在Swift中不可用iOS 7及更早版本的API已被弃用。 - djbp

1
单元格函数使用:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{

    var cell = tableView.dequeueReusableCellWithIdentifier(kLCellIdentifier) as UITableViewCell!
    if !cell {
        cell = UITableViewCell(style:.Default, reuseIdentifier: kLCellIdentifier)
    }
    cell.backgroundColor = UIColor.clearColor()
    cell.textLabel.text = arrData[indexPath.row]
    cell.image = UIImage(named: "\(arrImage[indexPath.row])")   
    cell.accessoryType  = UITableViewCellAccessoryType.DetailDisclosureButton
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell
}

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