繁体   English   中英

迅速:reloadData后,tableview不起作用

[英]swift: tableview does not work after reloadData

我在viewcontroller中有一个tableview,并且因为我需要为另一个表重用大多数代码,所以我创建了一个额外的类:

class StatisticsViewDelegate: NSObject, UITableViewDelegate, UITableViewDataSource {

    var defaultList:[String]
    var infolist:[String] = []    
    var tableView:UITableView
    var controller:UIViewController?

    init(defaultList:[String], view:UITableView, controller:UIViewController?) {
        self.defaultList = defaultList
        self.controller = controller
        tableView = view        
        super.init()
        tableView.delegate = self
        tableView.dataSource = self
        loadTable()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return infolist.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "infocell", for: indexPath) as! TableViewCell
        // [fill cell]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // [...]
    }



    func loadTable() {

        DispatchQueue.global(qos: .userInitiated).async {
           //[...] 
           // in this case:
           self.infolist = self.defaultList
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }


}

并在我的UITViewController中的viewDidLoad()delegate = StatisticsViewDelegate(defaultList: defaultList, view: tableView, controller:self) delegate是ViewController的成员

现在,当我运行它时,函数func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)永远不会被调用。 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)被调用(在重装之前和之后)并返回正确的数字(在我的情况下为4)。 TableView根本不可见。 我的错误在哪里?

也许您可以使用子分类策略来解决您的问题。 有很多引用传递给您的班级,如果您忘记清理这些内容,那么您的内存就会泄漏。 因此,我将建议以下简单示例。 您可以根据需要进行修改,然后告诉我您是否要这样做。 如果没有,请原谅我。

//This will be parent class that will handle all table methods, so you need to write only once the delegates and stuffs
class MyCommonTableController: UITableViewController {

var infoList = [String]()

// MARK: - TableView Delegate and Datsource Impl
override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return infoList.count
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 55.0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = infoList[indexPath.row]
    return cell
}

}

从上面的MyCommonTableController直接子类化的第一个类

//In here we just have to know the data and set the infoList from parent
class TheTableViewController: MyCommonTableController {


let defaultList = ["Data1","Data2","Data3"] //....etc


override func viewDidLoad() {
    super.viewDidLoad()

    //this is were I will set those
    infoList = defaultList

    //reload the table
    tableView.reloadData()
}


}

第二个类直接从上面的MyCommonTableController子类MyCommonTableController 同样的过程在这里

class TheSecondTableViewController: MyCommonTableController {


let defaultList = ["List1","List2","List3"] //....etc


override func viewDidLoad() {
    super.viewDidLoad()

    //this is were I will set those
    infoList = defaultList

    //reload the table
    tableView.reloadData()
}


}

现在,您不再重复表方法。 您还可以获取表的引用并在规范表视图中使用

@IBOutlet weak var theTable: UITableView!


let defaultList = ["List1","List2","List3"] //....etc
let commonTable = MyCommonTableController()

// MARK: - LifeCycle
override func viewDidLoad() {
    super.viewDidLoad()

    commonTable.infoList = defaultList
    commonTable.tableView = theTable
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM