Swift 搜索栏筛选的tableViewCells didSelectItemAt indexPath显示在导航栏标题中

Swift 搜索栏筛选的tableViewCells didSelectItemAt indexPath显示在导航栏标题中,swift,uitableview,uinavigationbar,filtering,uisearchbar,Swift,Uitableview,Uinavigationbar,Filtering,Uisearchbar,从搜索栏筛选的tableView单元格实现DidSelectRowatineXpath时遇到问题。当我基于searchbar textDidChange更新tableView列表,并执行到另一个ViewController的显示序列时,navigationBar标题始终显示0中未过滤的tableView。换句话说,我希望导航标题显示搜索结果tableView的didSelectAtIndex中的文本(而不是非筛选tableView中的原始indexPath单元格文本)。希望这是有意义的,并提前感

从搜索栏筛选的tableView单元格实现DidSelectRowatineXpath时遇到问题。当我基于searchbar textDidChange更新tableView列表,并执行到另一个ViewController的显示序列时,navigationBar标题始终显示0中未过滤的tableView。换句话说,我希望导航标题显示搜索结果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
    }
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let placeDetailsVC = CurrentUserPlaceDetailsVC()
        // Get Cell Label
        placeDetailsVC.navigationTitle = placeList[indexPath.row].place
        show(placeDetailsVC, sender: self)

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

class CurrentUserPlaceDetailsVC: UIViewController {
    var navigationTitle: String?

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

    } 

谢谢,但仍然不起作用…我想知道在搜索栏显示过滤后的tableView时是否需要重置indexPath?我添加了一个图像供参考。你能给我看一下你的数据源方法实现吗??numberOfRows&cellForRow。还有一件事,tableView是否也显示了筛选结果?如果没有,那么您是在搜索一个方法来筛选tableView,还是当前正在成功显示筛选器,但希望将数据从选择单元格传递到下一个视图控制器?当然,我在原始帖子中更新了我的代码示例以显示相关方法。My tableView成功地在searchBar textDidChange上显示筛选结果,但在点击筛选结果单元格时,它没有将正确的单元格文本推送到下一个VC navigationBar标题。因此,在我所附的图片中,当我输入“B”时,点击“泰国曼谷”,表格过滤器显示泰国曼谷,它会推送到下一个VC,但导航栏标题没有显示“泰国曼谷”。而是显示0的原始未筛选indexPath.item(未筛选tableView中的第一项)。解决了我自己的问题。。。不确定这是否是最好的方法,但目前它仍然有效。请参阅我的didSelectRowAt indexPath方法的编辑代码。