2017-05-19 13 views
0

enter image description hereenter image description here searchBarフィルタテーブルビューセルからdidSelectRowAtIndexPathを実装する際に問題があります。検索バーのtextDidChangeに基づいてtableViewリストを更新し、別のViewControllerにショーセグを実行すると、ナビゲーションバータイトルは常にフィルタリングされていないtableViews indexPath 0を表示します。つまり、ナビゲーションタイトルにはテキストが表示されます検索結果tableViewのdidSelectAtIndex(フィルタリングされていないtableViewの元のindexPathセルテキストではありません)。うまくいけばそれは意味があり、事前に感謝します!searchBarフィルタリングされたtableViewCells didSelectItemAtナビゲーションバーの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) 
     } 
    } 
} 

答えて

0

今後登場するViewControllerに文字列を作成します。今、代わりに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) 

    } 
+0

searchBarがフィルタリングされたtableViewを表示しているときにindexPathをリセットする必要があるのでしょうか?参考のために画像を追加しました。 – user3708224

+0

データソースメソッドの実装を表示できますか? numberOfRows&cellForRowもあります。 –

+0

さらにもう1つテーブルビューでフィルタ結果も表示されますか?そうでない場合は、tableViewをフィルタリングするメソッドを探しているか、現在はフィルタを表示していますが、select cellから次のView Controllerにデータを渡したいのですか? –

関連する問題