Swift 重新加载表数据时未调用CellForRowAtIndexPath
我已经研究了很多答案,比如,但是我仍然被卡住了 我有一个带有collectionView和searchController的viewcontroller。当用户搜索时,我希望在collectionView顶部显示UITableView。我正在尝试以编程方式添加tableview b/c,这是一个简单的文本标签 我能够在tableviewcontroller类的viewdidload中显示虚拟数组的搜索结果。但是,当我得到实际结果时,我只能在numberOfRows函数中打印它们 非常感谢您的帮助,以下是我的代码: 这是我的TableView类:Swift 重新加载表数据时未调用CellForRowAtIndexPath,swift,uitableview,uisearchcontroller,Swift,Uitableview,Uisearchcontroller,我已经研究了很多答案,比如,但是我仍然被卡住了 我有一个带有collectionView和searchController的viewcontroller。当用户搜索时,我希望在collectionView顶部显示UITableView。我正在尝试以编程方式添加tableview b/c,这是一个简单的文本标签 我能够在tableviewcontroller类的viewdidload中显示虚拟数组的搜索结果。但是,当我得到实际结果时,我只能在numberOfRows函数中打印它们 非常感谢您的帮助
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,它从此处不会更改
您在
HashtagSearchList
中初始化了hashtagListTableview
,但没有添加布局约束。默认情况下,它将具有.zero
帧,并且不会显示在屏幕上
我猜屏幕上的表视图是来自UITableViewController
的tableView
。这就是为什么调用self.hashtagSearchController.hashtagListTableview.reloadData()
时什么也没发生
要修复它,请尝试使用tableView
而不是hashtagListTableview
。替换
self.hashtagSearchController.hashtagListTableview.reloadData()
与
检查未重新加载的tableview框架。Tableview将只重新加载帧中当前可见的单元格。谢谢您的帮助!我添加了一些约束,并手动设置了框架大小之前没有运气,但现在它是有意义的,为什么,我实际上使用了不同的tableview。。。我没想过。再次感谢,让我开心:)
self.hashtagSearchController.tableView.reloadData()