gpt4 book ai didi

swift - searchBar过滤的tableViewCells didSelectItemAt indexPath显示在navigationBar标题中

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

enter image description here enter image description here我在从 searchBar 过滤的 tableView 单元格实现 didSelectRowAtIndexPath 时遇到问题。当我根据搜索栏textDidChange更新tableView列表,并对另一个ViewController执行显示segue时,navigationBar标题始终显示未过滤的tableViews indexPath 0。换句话说,我想让导航标题显示来自的文本搜索结果 tableView 的 didSelectAtIndex (不是来自未过滤的 tableView 的原始 indexPath 单元格文本)。希望这是有道理的,并提前致谢!

// viewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()

searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.placeholder = " Search Places..."
searchBar.sizeToFit()
searchBar.isTranslucent = false
searchBar.backgroundImage = UIImage()
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
navigationItem.titleView = searchBar

ref = FIRDatabase.database().reference()
fetchPlaces()

placesClient = GMSPlacesClient.shared()
locationManager.requestAlwaysAuthorization()

tableView.allowsMultipleSelectionDuringEditing = true

}

var placeList = [Place]()
var placesDictionary = [String: Place]()

// fetch places for tableView method
func fetchPlaces() {
let uid = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference().child("users").child(uid!).child("Places")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let place = Place()
place.setValuesForKeys(dictionary)

if let addedBy = place.addedBy {
self.placesDictionary[addedBy] = place
self.placeList.insert(place, at: 0)
}
//this will crash because of background thread, so lets call this on dispatch_async main thread
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}

// search variables
lazy var searchBar:UISearchBar = UISearchBar()
var isSearching = false
var filteredData = [Place]()

// searchBar method
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
tableView.reloadData()
} else {
isSearching = true
filteredData = placeList.filter({$0.place?.range(of: searchBar.text!) != nil})
tableView.reloadData()
}
}

// tableView methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return filteredData.count
}
return placeList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
if isSearching {
cell.textLabel?.text = filteredData[indexPath.row].place
} else {
cell.textLabel?.text = placeList[indexPath.row].place
}
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let placeDetailsVC = CurrentUserPlaceDetailsVC()
if isSearching == false {
placeDetailsVC.navigationTitle = placeList[(indexPath.row)].place
show(placeDetailsVC, sender: self)
} else {
placeDetailsVC.navigationTitle = filteredData[(indexPath.row)].place
show(placeDetailsVC, sender: self)
}
}
}

最佳答案

在即将到来的 ViewController 中创建一个字符串。

class CurrentUserPlaceDetailsVC: UIViewController {
var navigationTitle: String?

override func viewDidLoad(){
super.viewDidLoad()
self.navigationItem.title = navigationTitle
}
}

现在,您应该先将标题分配给该字符串,然后再将其分配给 viewController 的 viewDidLoad 方法中的 navigationBar,而不是直接将标题分配给 navigationBar。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let placeDetailsVC = CurrentUserPlaceDetailsVC()
// Get Cell Label
placeDetailsVC.navigationTitle = placeList[indexPath.row].place
show(placeDetailsVC, sender: self)

}

关于swift - searchBar过滤的tableViewCells didSelectItemAt indexPath显示在navigationBar标题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44079490/

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