簡體   English   中英

外部類中的 UITableView 委托和數據源不起作用

[英]UITableView delegate and datasource in external class does not work

我有一個簡單的項目,將 UITableView 添加為當前視圖的子視圖,以及創建委托和數據源的外部 tableviewcontroller 類。

問題是除了委托 didSelectedRowAtIndexPath 之外,所有工作都正常,當我在單行中單擊時,所有 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() 被解除分配。 UITableView 上的dataSourcedelegate屬性很弱。 因此,因為您只將 TbController 分配給局部變量和弱屬性,它將被釋放。

此外, UITableViewController 已經為您創建了一個 TableView。 “重新創建”它不是一個好習慣。 我寧願使用 UITableViewController 中的 TableView 並將 UITableViewController 的視圖作為子視圖添加到 UIViewController 的視圖中。

要修復,您需要 TbController 的實例變量。

帶有 ivar 的 TbController 示例代碼並使用 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM