【问题标题】:CellForRowAtIndexPath not Called when reloading table data重新加载表数据时未调用 CellForRowAtIndexPath
【发布时间】:2020-02-14 03:59:44
【问题描述】:

我已经解决了很多像this one 这样的答案,但我仍然卡住了。

我有一个带有 collectionView 和 searchController 的视图控制器。当用户搜索时,我想在 collectionView 的顶部显示一个 UITableView。我正在尝试以编程方式添加表格视图 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 的 tableview,它永远不会从这里改变

【问题讨论】:

  • 查看未重新加载的 tableview 的框架。 Tableview 将仅重新加载当前在框架中可见的那些单元格。

标签: swift uitableview uisearchcontroller


【解决方案1】:

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

我猜屏幕上的表格视图是来自UITableViewControllertableView。这就是为什么当你打电话给self.hashtagSearchController.hashtagListTableview.reloadData() 时什么都没有发生的原因。

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

self.hashtagSearchController.hashtagListTableview.reloadData()

self.hashtagSearchController.tableView.reloadData()

【讨论】:

  • 感谢您的收获!我已经添加了一些约束,并且之前没有运气手动设置帧大小,但现在它是有道理的,为什么我实际上使用的是不同的 tableview ......没有考虑到这一点。再次感谢,让我开心:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 2011-12-04
相关资源
最近更新 更多