【问题标题】:Linking SwiftViewController to xib containing a UITableView将 SwiftViewController 链接到包含 UITableView 的 xib
【发布时间】:2014-11-05 18:19:48
【问题描述】:

大家好,提前感谢你们,我是 Interface builder 和 Swift 的新手,所以它可能是一种或多种技术的组合。

我要做的是一个简单的 UIView,它在界面构建器中包含一个 UITableView,并将它链接到我在 swift 中创建的 viewController。我得到的错误是休闲: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

我相信问题是因为我在 UIViewController 上调用 numberOfRowsInSection 而不是我的类 SearchesMainViewController。

SearchesMainViewController.swift 看起来像这样:

import Foundation
import UIKit

class SearchesMainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var contentTableView: UITableView?

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = contentTableView?.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}
} 

这是我在我的 xib 上为 File's Owner 建立的价值观和联系

再次感谢您的宝贵时间和帮助,我真的很感激 :)

编辑 这就是我在 AppDelegate.swift 中初始化 SearchesMainView 的方式

【问题讨论】:

    标签: ios iphone uitableview swift interface-builder


    【解决方案1】:

    可能是viewDidLoad中的这段代码,修复你的问题:

    contentTableView?.delegate = self
    contentTableView?.dataSource = self
    

    【讨论】:

    • 我试了一下,没有成功,出现同样的错误。看起来插座和连接正在尝试在 UIViewController 而不是 SearchesMainViewController 上进行,我相信在调用此生命周期方法之前存在错误
    • 如何初始化 SearchesMainViewController?
    • 在 AppDelegate.swift 我有:让 searchMainView = UIViewController(nibName: "SearchesMainView", bundle: nil)
    • 你可能正在做一些事情,需要的 init(coder aDecoder: NSCoder) { super.init() } override func viewDidLoad() { } 都没有被调用
    • 我认为你的初始化是错误的:let searchesMainView = SearchesMainViewController(nibName: "SearchesMainView", bundle: nil)
    猜你喜欢
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多