Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift中UITableView的动态数据源/委托_Swift_Swift2 - Fatal编程技术网

swift中UITableView的动态数据源/委托

swift中UITableView的动态数据源/委托,swift,swift2,Swift,Swift2,我需要根据特定条件设置不同的对象作为表视图的数据源和委托 但我无法分配tableview的数据源/委托,因为它会引发一些错误 无法分配NSObject类型的值?是否为UITableViewDelegate类型的值 我确实检查了问答,但这不起作用 var dataSourceDelegate:NSObject? class RootViewController: UIViewController { ... override func viewDidLoad() { dataSou

我需要根据特定条件设置不同的对象作为表视图的数据源和委托

但我无法分配tableview的数据源/委托,因为它会引发一些错误

无法分配NSObject类型的值?是否为UITableViewDelegate类型的值

我确实检查了问答,但这不起作用

var dataSourceDelegate:NSObject?
class RootViewController: UIViewController {
...
override func viewDidLoad() {
        dataSourceDelegate = TableDataSourceDelegate()
        // Table View
        tableView = UITableView()
        tableView!.setTranslatesAutoresizingMaskIntoConstraints(false)
        tableView!.dataSource = dataSourceDelegate 
        // Cannot assign a value of type NSObject? to a value of type UITableViewDataSource?
        tableView!.delegate = dataSourceDelegate
        // Cannot assign a value of type NSObject? to a value of type UITableViewDelegate?
        view.addSubview(tableView!)

        // Constraints
        var views:[String:UIView] = ["table":tableView!]
        var hTableConstraint = "H:|[table]|"
        var vConstraint = "V:|[table]|"
        view.addConstraintsToView([hTableConstraint, vConstraint], view: view, viewVariables: views)
    }
...
}
这是数据源/委托类

class TableDataSourceDelegate:NSObject, UITableViewDataSource, UITableViewDelegate {
    // MARK: Datasource

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return UITableViewCell()
    }

    // MARK: Delegates
}

NSObject?不符合UITableViewDelegate,也不符合UITableViewDataSource。您应该像这样创建协议

protocol GeneralDataSource: UITableViewDataSource, UITableViewDelegate {}
然后所有数据源都应该符合该协议

class MyDataSource: NSObject, GeneralDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return UITableViewCell()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
}
那么你可以这样使用它

var myDataSource: GeneralDataSource?

override func viewDidLoad() {
    super.viewDidLoad()

    self.myDataSource = MyDataSource()
    self.tableView.delegate = self.myDataSource
}

这就是您的
TableDataSourceDelegate
的外观:

import UIKit

class TableDataSourceDelegate: NSObject {
}

extension TableDataSourceDelegate: UITableViewDataSource {
    @objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "defaultCell")
        cell.textLabel?.text = "test"
        return cell
    }
}

extension TableDataSourceDelegate: UITableViewDelegate {
    // your delegate implementation here
}
和视图控制器实现

import UIKit

// The typealias definition
typealias TVDataSourceDelegate = protocol<UITableViewDataSource, UITableViewDelegate>

class ViewController: UIViewController {

    var dataSourceDelegate: TVDataSourceDelegate?
    var tableView: UITableView?

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSourceDelegate = TableDataSourceDelegate()
        // Table View
        tableView = UITableView()
        tableView!.translatesAutoresizingMaskIntoConstraints = false
        tableView!.dataSource = dataSourceDelegate
        tableView!.delegate = dataSourceDelegate
        view.addSubview(tableView!)

        // other code ...
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
导入UIKit
//类型别名定义
typealias TVDataSourceDelegate=协议
类ViewController:UIViewController{
var dataSourceDelegate:TVDataSourceDelegate?
var tableView:UITableView?
重写func viewDidLoad(){
super.viewDidLoad()
dataSourceDelegate=TableDataSourceDelegate()
//表视图
tableView=UITableView()
tableView!.translatesAutoresizingMaskIntoConstraints=false
tableView!.dataSource=dataSourceDelegate
tableView!.delegate=dataSourceDelegate
view.addSubview(tableView!)
//其他代码。。。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}

尽管如此,我还是建议将数据源和委托对象分开(例如,将符合委托协议的代码放入视图控制器的代码中。

我必须创建一个新类才能使其工作。这也可以工作,但我接受另一个,因为它已在前面发布,希望我能更新投票。