0
enter 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)
}
}
}
고맙지 만 아직 작동하지 않습니다 ... searchBar가 필터링 된 tableView를 표시 할 때 indexPath를 재설정해야합니까? 참조 용 이미지를 추가했습니다. – user3708224
데이터 소스 메서드 구현을 보여줄 수 있습니까 ?? numberOfRows & cellForRow도 있습니다. –
그리고 한 가지 더 tableView 필터 결과도 표시합니까? 그렇지 않다면 당신은 tableView를 필터링하는 방법을 찾고 있습니까 아니면 현재 필터를 성공적으로 표시하고 있지만 선택 셀에서 다음보기 컨트롤러로 데이터를 전달하고 싶습니까? –