UITableViewDataSource方法不起作用

时间:2019-04-25 20:49:14

标签: swift tableview datasource

未触发dataSource方法 我的tableView保持为空。 使用断点,我可以看到数据源从未执行 我看不到原因和方式

import UIKit

class SearchResultsViewController: BaseViewController {
    @IBOutlet var tableView: UITableView!
    let viewModel = SearchResultsViewModel()
    let searchController = UISearchController(searchResultsController: nil)
    var data: [Result] = []
    var searchActive : Bool = false
    var action: Action?

    lazy var dataSource: SearchResultDataSource? = {
        guard let results = self.viewModel.data else { return nil }
        return SearchResultDataSource(items: results)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        configureView()
        loadData()
    }
    private func loadData() {

        viewModel.search(term: "eminem", mediaType: .music(entity: .song, attribute: nil), country: .unitedStates, completion: { err in
            if err == nil {
                guard let results = self.viewModel.data else { return  }
                self.tableView.dataSource =  SearchResultDataSource(items: results)
                self.tableView.reloadData()
            } else {
                AlertDialogView.build(with: String(describing: err?.errorDescription), vc: self)
            }
        })
    }
    private func configureView() {
        tableView.register(UINib(nibName: "SearchResultTableViewCell", bundle: nil), forCellReuseIdentifier: SearchResultTableViewCell.identifier)
        configureSearchBarForTableView()
        tableView.delegate = self
    }
}

**这是我的dataSource类** 不会触发dataSource方法 我的tableView保持为空。 使用断点,我可以看到数据源从未执行 我看不到原因和方式

在一个单独的文件中,这是我实现行数等的地方...

class SearchResultDataSource: NSObject {
    private var results: [Result]
    init(items: [Result]) {
        self.results = items
        super.init()
    }
    // MARK: - Helper
    func update(with data: [Result]) {
        results = data
    }
    func result(at indexPath: IndexPath) -> Result {
        return results[indexPath.row]
    }
}

// MARK: - UITableDataSource
extension SearchResultDataSource: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return results.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        guard let cell = tableView.dequeueReusableCell(withIdentifier: SearchResultTableViewCell.identifier, for: indexPath) as? SearchResultTableViewCell else {
//            return UITableViewCell()
//        }


        guard let cell = tableView.dequeueReusableCell(withIdentifier: SearchResultTableViewCell.identifier, for: indexPath) as? UITableViewCell else {
            return UITableViewCell()
        }
        //let result = results[indexPath.row]
        //let viewModel = SearchCellViewModel(with: result)
        //cell.configure(with: viewModel)
        cell.textLabel?.text = "cell"
        return cell
    }
}

2 个答案:

答案 0 :(得分:1)

您要保留对数据源的强烈引用,将其作为控制器上的属性,以使其不会被释放。

根据您现有的代码,您可以使用一个空数组将其初始化,如下所示:

class SearchResultsViewController: BaseViewController {
    ...

    var dataSource = SearchResultDataSource(items: [])

    ...
}

然后在loadData()函数内部,可以使用搜索结果调用update(with :)方法。

private func loadData() {
    viewModel.search(term: "eminem", mediaType: .music(entity: .song, attribute: nil), country: .unitedStates, completion: { err in
        if err == nil {
            guard let results = self.viewModel.data else { return  }
            self.dataSource.update(with: results)
            self.tableView.reloadData()
        } else {
            AlertDialogView.build(with: String(describing: err?.errorDescription), vc: self)
        }
    })
}

答案 1 :(得分:0)

在此行

self.tableView.dataSource =  SearchResultDataSource(items: results)

您正在创建新的SearchResultDataSource,该结果将被销毁。您需要将其保存到self.dataSource中,以便对其有很强的引用。可能需要从

lazy var dataSource: SearchResultDataSource? = {

中删除lazy
相关问题