Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Ios 外部类中的UITableView委托和数据源不工作_Ios_Swift_Uitableview - Fatal编程技术网

Ios 外部类中的UITableView委托和数据源不工作

Ios 外部类中的UITableView委托和数据源不工作,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个简单的项目,其中UITableView作为当前视图的子视图添加,外部tableviewcontroller类构成委托和数据源 问题是,所有工作都正常,除非委托未选择LowatineIndexPath,当我单击一行时,所有tableview都变为白色,下面是代码: 主要类别: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad(

我有一个简单的项目,其中UITableView作为当前视图的子视图添加,外部tableviewcontroller类构成委托和数据源

问题是,所有工作都正常,除非委托未选择LowatineIndexPath,当我单击一行时,所有tableview都变为白色,下面是代码:

主要类别:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let cont:Tbcontroller = Tbcontroller()
    let tableViewInner = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width-(self.view.frame.width/6), height: 180))
    tableViewInner.scrollEnabled = false
    tableViewInner.separatorStyle = .None
    tableViewInner.allowsSelection = true
    tableViewInner.userInteractionEnabled = true
    cont.type = 1
    cont.setCurrentTableView(tableViewInner)
    self.view.insertSubview(tableViewInner, atIndex: self.view.subviews.count+1)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
外部类委托数据源:

import UIKit

class Tbcontroller: UITableViewController {

var type:Int = 1

override func viewDidLoad() {
    super.viewDidLoad()


}

func getCell() -> UITableViewCell{
    let presentCell: UITableViewCell = UITableViewCell()
    return presentCell

}

func setCurrentTableView(table:UITableView){

    self.tableView = table
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 4
}

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

    let cell = getCell()
    cell.textLabel?.text = "ciao"
    cell.textLabel?.font = UIFont.systemFontOfSize(13)

    return cell

}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return 100

}

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

    print("clicked")

}

}

单击时,我看不到“单击”的输出。

您的主要问题是TbController()无法释放数据源和
委托
属性较弱。因此,因为您只将TbController分配给局部变量和弱属性,所以它将被解除分配

此外,UITableViewController已经为您创建了一个TableView。“重现”它不是一种好的做法。我宁愿使用UITableViewController中的TableView,并将UITableViewController的视图作为子视图添加到UIViewController的视图中

要修复此问题,您需要TbController的实例变量

TbController的ivar示例代码,并使用TbController的UITableView。

 import UIKit

    class ViewController: UIViewController {

    var cont: Tbcontroller = Tbcontroller()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        cont.type = 1
        self.view.addSubview(cont.view)
    }

}

    import UIKit

    class Tbcontroller: UITableViewController {

    var type:Int = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.scrollEnabled = false
        tableView.separatorStyle = .None
        tableView.allowsSelection = true
        tableView.userInteractionEnabled = true

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

    func getCell() -> UITableViewCell{
        let presentCell: UITableViewCell = UITableViewCell()
        return presentCell

    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 4
    }

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

        let cell = getCell()
        cell.textLabel?.text = "ciao"
        cell.textLabel?.font = UIFont.systemFontOfSize(13)

        return cell

    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 100

    }

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

        print("clicked")

    }

}

多亏了Carien,它工作得很好,我将uitableviewcontroller更改为:

: NSObject, UITableViewDataSource, UITableViewDelegate