【问题标题】:Programmatically UITableView using swift使用 swift 以编程方式 UITableView
【发布时间】: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)

有什么想法吗?

【问题讨论】:

标签: ios uitableview swift


【解决方案1】:

在 Xcode 6 Beta 4 中

删除

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

会成功的。这是由 Obj-C 和 Swift 之间不同的初始化程序行为引起的。您已经创建了一个指定的初始化程序。如果你删除它,所有的初始化器都会被继承。

根本原因可能在-[UITableViewController initWithStyle:]调用

[self initWithNibName:bundle:]

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

【讨论】:

  • 我不得不调整 cellForRowAtIndexPath 中的其他内容,但现在它可以工作了。谢谢苏丹!
  • 你调整了什么?我假设你应该使用 'tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")' 因为你没有使用 xib 的单元格是吗?
  • 这最终在 Swift 2 中得到修复,从 XCode 7.0 beta (7A120f) 开始。现在可以直接使用 super.init(style: style)...
【解决方案2】:

而不是

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

您可能会觉得这很方便:

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

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

【讨论】:

  • 在 Xcode 6 beta 5 中,在这种情况下,您不能再声明无参数的便利初始化程序。见:stackoverflow.com/questions/25139494/…
  • 已经找了一个小时了。非常感谢!!适用于 Xcode 7.0 和 Swift 2.0
【解决方案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 已弃用且不可用:'text' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift
【解决方案4】:

细胞功能使用:

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
}

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 2019-06-29
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2017-02-04
    相关资源
    最近更新 更多