2017-05-19 8 views
0

enter image description hereenter image description here searchBar 필터링 된 tableView 셀에서 didSelectRowAtIndexPath를 구현하는 데 문제가 있습니다. 검색 막대 textDidChange를 기반으로 tableView 목록을 업데이트하고 다른 ViewController에 대한 표시 세부를 수행하면 탐색 모음 제목은 항상 필터링되지 않은 tableViews indexPath 0을 표시합니다. 즉, 탐색 제목에 텍스트를 표시하고 싶습니다. 검색 결과 tableView의 didSelectAtIndex (필터링되지 않은 tableView의 원본 indexPath 셀 텍스트가 아님). 희망적으로 그것은 의미가 있고, 미리 감사드립니다!searchBar 필터링 된 tableViewCells didSelectItemAt navigationBar의 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에서 문자열을 만듭니다.

class CurrentUserPlaceDetailsVC: UIViewController { 
    var navigationTitle: String? 

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

지금 대신 내비게이션 바에 직접 제목을 할당하는 당신은 당신의 ViewController의있는 viewDidLoad 방법에 내비게이션 바 먼저 해당 문자열에 다음을 할당해야합니다.

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

그리고 한 가지 더 tableView 필터 결과도 표시합니까? 그렇지 않다면 당신은 tableView를 필터링하는 방법을 찾고 있습니까 아니면 현재 필터를 성공적으로 표시하고 있지만 선택 셀에서 다음보기 컨트롤러로 데이터를 전달하고 싶습니까? –