sans chercher, quand j'ai tapé n'importe quelle cellule, l'alerte montre le nom de famille correct. Cependant, d'une manière ou d'une autre, lorsque je recherche un nom et que je tape sur sa cellule, l'alerte affiche un mauvais nom de famille. Dans tous les cas (je veux dire, je cherche n'importe quoi) l'alerte montre toujours le nom de famille Quuen. Je pense à cause de quand je cherche, le résultat devient toujours le premier index de ma tableview.Une confusion à propos de SearchBar et Tableview
Mes codes:
import UIKit
struct Human {
var name = String()
var surname = String()
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var members = [Human]()
var filteredData = [String]()
var inSearchMode = false
var names = ["Oliver","Harold","John","Thea","Felicity"]
var surnames = ["Queen","Finch","Reese","Queen","Smoak"]
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<names.count {
members.append(Human(name: names[i],surname: surnames[i]))
}
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
}
// MARK: - UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredData.count
}
return members.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DataCell {
let text: String!
if inSearchMode {
text = filteredData[indexPath.row]
} else {
text = members[indexPath.row].name
}
cell.congigureCell(text: text)
return cell
} else {
return UITableViewCell()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
view.endEditing(true)
tableView.reloadData()
} else {
inSearchMode = true
filteredData = names.filter { $0.lowercased().contains(searchBar.text!.lowercased()) }
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showAlert(title: "Surname is...", msg: members[indexPath.row].surname)
}
}
extension UIViewController {
func showAlert(title: String, msg: String) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
et captures d'écran sont ici:
Enfin, comment puis-je résoudre ce problème?
Votre code affiché n'est pas utile pour analyser. Essayez de poster plus de code comme la façon dont vous filtrez les résultats après la recherche, etc., –
Je ne pense pas que cela soit lié à votre problème, mais "Queen" apparaît deux fois dans votre tableau de noms. – Koen