gpt4 book ai didi

ios - UITableView 调用 cellFor row 但不在 UI 中绘制

转载 作者:行者123 更新时间:2023-11-30 11:40:30 26 4
gpt4 key购买 nike

我正在调用 API 来搜索“ friend ”/(UserProfile 类)并希望将其显示在 tableView 中。我还在本地 session 中搜索工作正常的地方。

但是切换到 friend 分段控件(见下文)后,输入用户名并按回车键, friend 模式下的 tableView 保持为空。但日志表明已找到它并且 cellForRowAt: 返回一些内容。当我第二次输入查询的一个字母时, friend 只会出现在我的 tableView 中(没有对 API 的查询,因为我没有按 Enter 键)。

认为它可能是异步的东西,但看起来不错,附上了日志。

//
// FilterViewController.swift


import Foundation
import UIKit

class FilterViewController: UIViewController {
enum SearchState: String {
case place = "Search for a place"
case friend = "Search for a friend"
}
var searchState: SearchState = .place
var placesResult: Array<Place> = []
var friendResult: Array<UserProfile> = []
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
self.placesResult = Session.shared.places
self.searchBar.isTranslucent = true
self.searchBar.autocapitalizationType = .none
// self.searchBar.searchBarStyle = .prominent
self.searchBar.barTintColor = UIColor.clear
self.searchBar.scopeButtonTitles = [SearchState.place.rawValue, SearchState.friend.rawValue]
}

override func viewDidAppear(_ animated: Bool) {
self.searchBar.showsScopeBar = true
self.searchBar.becomeFirstResponder()
self.searchBar.tintColor = UIColor.white
}
}

extension FilterViewController: UISearchBarDelegate {

// The places part works fine
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
self.friendResult = []
self.placesResult = []
return
}
guard self.searchState == .place else {
return
}
self.placesResult = Session.places(matching: searchText)
print("FilterView search query: \(searchText)")
tableView.reloadData()
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
/// We use it only for friends because for places we search on text change
guard self.searchState == .friend else {
return
}
//// HERE IS THE PART WHERE I QUERY.
let query = UserProfileQuery(forName: searchBar.text!)
query.performQuery() { results, error in
print("Search User results: \(results.count) users")
DispatchQueue.main.async {
self.friendResult.removeAll()
print("reload")
for result in results {
print(">>> Results: \(result["name"].debugDescription)")
let userProfile = UserProfile(fromRecord: result)
self.friendResult.append(userProfile)
}
print("calling tableView reloadSections. \(self.friendResult.count) friends")
// self.tableView.reloadSections([0], with: .left)
//self.tableView.reloadSections([0], with: .left)
self.tableView.reloadData()
//// AFTER THIS THE TABLEVIEW CALLS THE DATA SOURCE. ALL GOOD SO FAR
}
}

}



func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.text = ""
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.dismiss(animated: true, completion: nil)
}

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
switch selectedScope {
case 0:
self.searchState = .place
self.searchBar.placeholder = SearchState.place.rawValue
case 1:
self.searchState = .friend
self.searchBar.placeholder = SearchState.friend.rawValue
default:
print("Error: searchbar in FilterViewController has unknown scope: \(selectedScope)")
}
}

}


extension FilterViewController: UITableViewDelegate {


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("willDisplay cell: \(cell.debugDescription)")
}

}

extension FilterViewController: UITableViewDataSource {

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch self.searchState {
case .place:
print("Place \(self.placesResult.count)")
return self.placesResult.count
case .friend:
print("numberOfRowsInSection: \(self.friendResult.count) friends")
return self.friendResult.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellForRowAt: for state \(self.searchState) \(self.friendResult.count)")
switch self.searchState {
case .place:
let cell = self.tableView.dequeueReusableCell(withIdentifier: "placeCell")!
cell.textLabel?.text = self.placesResult[indexPath.item].name
print("return place cell")
return cell
case .friend:
let cell = self.tableView.dequeueReusableCell(withIdentifier: "friendCell")!
cell.textLabel?.text = self.friendResult[indexPath.item].name
//cell.userProfile = self.friendResult[indexPath.item]
//cell.title.text = cell.userProfile.name
print("return cell: \(cell.debugDescription)")
return cell
}

}
}

这是上面打印的日志:

numberOfRowsInSection: 0 friends
numberOfRowsInSection: 0 friends
numberOfRowsInSection: 0 friends
numberOfRowsInSection: 0 friends
numberOfRowsInSection: 0 friends
Search User results: 1 users
reload
>>> Results: Optional(johnappleseed)
calling tableView reloadSections. 1 friends
numberOfRowsInSection: 1 friends
cellForRowAt: for state friend 1
return cell: <UITableViewCell: 0x7fdd15f02400; frame = (0 0; 300 44); text = 'johnappleseed'; clipsToBounds = YES; layer = <CALayer: 0x604001e25160>>
willDisplay cell: <UITableViewCell: 0x7fdd15f02400; frame = (0 0; 300 44); text = 'johnappleseed'; clipsToBounds = YES; autoresize = W; tintColor = UIExtendedSRGBColorSpace 0 0 1 1; layer = <CALayer: 0x604001e25160>>
numberOfRowsInSection: 1 friends
cellForRowAt: for state friend 1
return cell: <UITableViewCell: 0x7fdd16651a00; frame = (0 0; 300 44); text = 'johnappleseed'; clipsToBounds = YES; layer = <CALayer: 0x60000182b4e0>>
willDisplay cell: <UITableViewCell: 0x7fdd16651a00; frame = (0 0; 300 44); text = 'johnappleseed'; clipsToBounds = YES; autoresize = W; tintColor = UIExtendedSRGBColorSpace 0 0 1 1; layer = <CALayer: 0x60000182b4e0>>
numberOfRowsInSection: 1 friends
cellForRowAt: for state friend 1
return cell: <UITableViewCell: 0x7fdd15802000; frame = (0 0; 300 44); text = 'johnappleseed'; clipsToBounds = YES; layer = <CALayer: 0x604001e2eec0>>
willDisplay cell: <UITableViewCell: 0x7fdd15802000; frame = (0 0; 300 44); text = 'johnappleseed'; clipsToBounds = YES; autoresize = W; tintColor = UIExtendedSRGBColorSpace 0 0 1 1; layer = <CALayer: 0x604001e2eec0>>

所以它肯定应该显示单元格,但显示的是:

enter image description here

然后,当我再次单击搜索栏并输入“J”时,它会显示单元格:

enter image description here

这里出了什么问题?

最佳答案

好的,我找到了。

所以我想其中一些与苹果为 iOS 11 支持 searchBar 所做的更改有关。用于UINavigationController .

我做错的是我分配了 searchBar成为tableViewHeader .

searchBar 时,这就中断了显示 scopeBar .

在这种情况下,如果 searchBar 则有效。是一个独立于 tableView 的东西,并与自动布局连接,所以 searchBar位于 tableView 之上.

在这种情况下,UIViewController该容器不是 UITableViewController

我在此线程中发布了更多详细信息:UISearchBarController iOS 11 issue - SearchBar and scope buttons overlap

所以布局在 IB 中应该是这样的:

enter image description here

在 IB 中看起来很奇怪,但它完全按照预期工作。猜猜这是 IB 中的错误

关于ios - UITableView 调用 cellFor row 但不在 UI 中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49340051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com