使用swift以编程方式UITableView [英] Programmatically UITableView using swift

查看:101
本文介绍了使用swift以编程方式UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用swift以编程方式创建一个简单的tableView,所以我在AppDelegate.swift上编写了这段代码:

I'm trying to create a simple tableView programmatically using swift, so I wrote this code on "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代码:

Basically I added the TableViewController creation and added to the window. And this is the TableViewController code:

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
}

}

因此,当我尝试运行代码时,收到此消息:

So, when I try to run the code I receive this message:


Xcode6Projects / TableSwift / TableSwift / TableViewController.swift: 12:12:致命错误:对类'TableSwift.TableViewController'使用未实现的初始化程序'init(nibName:bundle :)'

Xcode6Projects/TableSwift/TableSwift/TableViewController.swift: 12: 12: fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'TableSwift.TableViewController'

编译器执行


super.init(样式:样式)

super.init(style: style)

有什么想法吗?

推荐答案

在Xcode 6 Beta 4中

In Xcode 6 Beta 4

删除

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

可以解决问题。这是由Obj-C和Swift之间的不同初始化程序行为引起的。您已创建指定的初始化程序。如果你删除它,所有初始值设定项都将被继承。

will do the trick. This is caused by different initializer behavior between Obj-C and Swift. You have created a designated initializer. If you remove it, all initializers will be inherited.

根本原因可能在 - [UITableViewController initWithStyle:] 哪个电话

The root cause is probably in -[UITableViewController initWithStyle:] which calls

[self initWithNibName:bundle:]

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

I actually think this might be a bug in the way Obj-C classes are converted to Swift classes.

这篇关于使用swift以编程方式UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆