OGeek|极客世界-中国程序员成长平台

标题: ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:49
标题: ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController

我正在尝试创建一个具有如下泛型类型的 VC:

class SearchViewController<T>: UIViewControlle {
    @IBOutlet weak var tableView: UITableView!
    var delegate: SearchViewControllerDelegate?
    fileprivate var dataArray: [T] = []

...
 }

extension SearchViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return showAddRow ? 1 : dataArray.count

    }
}

但我收到此错误:

Non-'@objc' method 'tableView(_:numberOfRowsInSection' does not satisfy requirement of '@objc' protocol 'UITableViewDataSource'

我尝试过这样的 @objc 方法:

@objc(tableView:numberOfRowsInSection

但它没有奏效。我错过了什么?



Best Answer-推荐答案


您需要为 tableView 声明所有协议(protocol) ..

 func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
    return 1
}
private func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{

}

关于ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41998128/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4