将SwiftViewController链接到包含UITableView的xib

时间:2014-11-05 18:19:49

标签: ios iphone uitableview swift interface-builder

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

我想要做的是一个简单的UIView,它在界面构建器中包含一个UITableView,并将它链接到我在swift中创建的viewController。我得到的错误是休闲: 因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例

我认为问题是因为我在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 for File的所有者

上所做的值和连接

File Owners Identity tab File Owners Connections tab

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

修改 这就是我在AppDelegate.swift中初始化SearchesMainView的方法 enter image description here

1 个答案:

答案 0 :(得分:0)

可以在viewDidLoad中使用此代码,解决您的问题:

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