Je travaille sur une application de location de voiture, qui accepte l'entrée de l'utilisateur et de le mettre dans ma base de données SQLite
.Barre de recherche rafraîchit l'index de ma cellule de vue de table
À l'intérieur de mon viewview viewcontroller, il affiche les informations très bien.
Exemple:
Cell 1: Toyota, Philippines, 5000
Cell 2: Nissan, America, 1000
Cell 3: Mitsubishi, England, 2000
Le problème est, chaque fois que je cherche, par exemple, je cherchais la « angleterre », les seules données à droite, il affiche seulement l'emplacement. Donc, il affiche comme ceci:
Cell 1: Toyota, England, 5000
qui est Toyota
et 5000
vient de l'indice 0
. et la seule bonne donnée est l'Angleterre.
Mon résultat désiré chaque fois que je recherche « angleterre »: Cell 1: Mitsubishi, England, 2000
S'il vous plaît me aider à fixer le type de voiture et la vitesse de l'afficher aussi.
Ceci est mon code dans mon contrôleur de vue de la table:
import UIKit
import SQLite
class CarList: UIViewController, UITableViewDataSource, UITableViewDelegate,
UISearchBarDelegate {
@IBOutlet var tableView: UITableView!
@IBOutlet var searchBar: UISearchBar!
//variables I used to append my database
var carType = [String]()
var rate = [String]()
var location = [String]()
//variables I used to append my filtered data for searchbar
var filteredLocation: [String]!
var filteredCar: [String]!
var filteredRate: [String]!
var img = [UIImage(named: "Toyota"), UIImage(named: "Nissan")]
override func viewDidLoad() {
super.viewDidLoad()
searchBar.placeholder = "Search Location"
do{
let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileUrl = documentDirectory.appendingPathComponent("users").appendingPathExtension("sqlite3")
let database = try Connection(fileUrl.path)
Variables.database = database
}catch {
print(error)
}
//appending my database to my array
do{
let users = try Variables.database.prepare(Variables.rTable)
for user in users {
carType.append(user[Variables.rCar])
rate.append(user[Variables.rRate])
location.append(user[Variables.rLocation])
}
}catch{
print(error)
}
searchBar.delegate = self
//assigning the array to filtered data
filteredLocation = location
filteredCar = carType
filteredRate = rate
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredLocation.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CarListCell
cell.carType.text = "Car Type: " + filteredCar[indexPath.row]
cell.rate.text = "Location: " + filteredLocation[indexPath.row]
cell.carImage.image = UIImage(named: filteredCar[indexPath.row]+".jpg")
return (cell)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredLocation = searchText.isEmpty ? location : location.filter({(dataString: String) -> Bool in
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
//below is the code when I clicked on the table view cell, it will pass the data.
//but the filtered location is the only one working.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "info" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let controller = segue.destination as! CarInfo
controller.getCar = filteredCar[indexPath.row]
controller.getRate = filteredRate[indexPath.row]
controller.getLocation = filteredLocation[indexPath.row]
}
}
}
} // End Class
Et voici ma table de base de données dans le cas où vous avez besoin, je me suis séparé à l'aide Variables.swift.
import Foundation
import UIKit
import SQLite
class Variables: UIViewController{
var database: Connection!
let rTable = Table("rTable")
let rId = Expression<Int>("rId")
let rCar= Expression<String>("rCar")
let rLocation = Expression<String>("rLocation")
let rRate = Expression<String>("rRate")
}
Où vous utilisez la variable filteredData? – Hosny
désolé qui est censé être filterLocation, mais à l'origine filterData mais je l'ai édité pour l'expliquer facilement. –
Ok le problème est l'emplacement et le type de voiture sont séparés du pays alors vous devriez les faire – Hosny