[CellForRowAtIndexPath重载表数据时未调用

问题描述 投票:0回答:1

我已经完成了很多回答,例如this one,但我仍然受困。

我有一个带有collectionView和searchController的viewcontroller。当用户搜索时,我想在collectionView顶部显示一个UITableView。我正在尝试以编程方式添加tableview b / c,这是我正在显示的简单文本标签

我能够显示在tableviewcontroller类的viewdidload中具有的虚拟数组的搜索结果。但是,当我得到实际结果时,只能在numberOfRows函数中打印它们。

非常感谢所有帮助,这是我的代码:

这是我的TableView类:

import UIKit

class HashtagSearchList: UITableViewController {

    var hashtagListTableview = UITableView()
    var hashtagList = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        hashtagList = ["One", "Two", "Three"]
        hashtagListTableview.delegate = self
        hashtagListTableview.dataSource = self
    }

    // MARK: - Table view data source

    //I don't need this, but added it based on some answers... didn't help
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        //All these print statements show correct values
        print("List Count = \(hashtagList.count)")
        print("List of Strings: \(hashtagList)")
        print("frame size = \(tableView.frame)")
        return hashtagList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        //This print statement only fires on ViewdidLoad
        print("cellForRowAt = \(hashtagList[indexPath.row])")
        cell.textLabel?.text = hashtagList[indexPath.row]
        cell.backgroundColor = .red

        return cell
    }
}

这是我的ViewController / SearchController代码:

class ExploreVC: UIViewController {

    var hashtagSearchController = HashtagSearchList()

    override func viewDidLoad() {
        super.viewDidLoad()
        //Search Controller
        navigationItem.searchController = searchController
        searchController = UISearchController(searchResultsController: hashtagSearchController)
    }

    //......

    // SEARCH CONTROLLER

    extension ExploreVC: UISearchResultsUpdating {
        func updateSearchResults(for searchController: UISearchController) {
            let searchText = searchController.searchBar.text

            if searchText == "" { return }

            PostFirebase.getHashtagList(hashtag: searchText) { (hashtagList) in

                self.hashtagSearchController.hashtagListTableview.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)

                self.hashtagSearchController.hashtagList = hashtagList
                //self.hashtagSearchController.hashtagListTableview.reloadData()

                DispatchQueue.main.async {
                    self.hashtagSearchController.hashtagListTableview.reloadData()
                }
            }
        }
    }
}

这里是viewDidLoad中的表视图,从这里开始它永远不会改变enter image description here

swift uitableview uisearchcontroller
1个回答
0
投票

您在hashtagListTableview中初始化了HashtagSearchList,但没有添加布局约束。默认情况下,它将具有.zero框架,并且不会在屏幕上显示。

我想屏幕上的表格视图是tableView中的UITableViewController。这就是为什么当您呼叫self.hashtagSearchController.hashtagListTableview.reloadData()时什么也没发生的原因。

要解决它,请尝试使用tableView而不是hashtagListTableview。替换

self.hashtagSearchController.hashtagListTableview.reloadData()

with

self.hashtagSearchController.tableView.reloadData()
© www.soinside.com 2019 - 2024. All rights reserved.