2017-10-19 13 views
0

私はレンタカーアプリケーションで作業しています。ユーザーの入力を受け入れ、SQLiteデータベースに入れています。検索バーがテーブルビューセルのインデックスを更新しています

私のtableview viewcontrollerの中には、情報をうまく表示しています。

例:

Cell 1: Toyota, Philippines, 5000 
Cell 2: Nissan, America, 1000 
Cell 3: Mitsubishi, England, 2000 

問題は、それが表示されているだけ権利データのみ位置で、例えば、私は、「イングランド」を検索し、私が検索するたびに、です。だから、このように表示されます。Toyota5000ある

Cell 1: Toyota, England, 5000

はインデックス0から来ています。唯一の正しいデータはイングランドです。私は「イングランド」を検索したときに

私の希望する結果: Cell 1: Mitsubishi, England, 2000

もそれを表示するには、車のタイプとレートを固定し、私を助けてください。

これは私のテーブルビューコントローラ内部の私のコードです:

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 

そして、これは私が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") 

} 
+0

filteredData変数はどこで使用しますか? – Hosny

+0

申し訳ありませんが、filteredLocationになっていますが、もともとfilteredDataでしたが、簡単に説明するために編集しました。 –

+0

問題は場所と車種が国から分離されているためです。オブジェクトを作成する必要があります – Hosny

答えて

0

ではなく、あなたのデータのための3つの配列を作る、あなたは彼らのためにオブジェクトを作ることができ

class Car{ 
var carType:String? 
var location:String? 
var rate:String? 
} 

を次にむしろ

var filteredLocation: [String]! 
var filteredCar: [String]! 
var filteredRate: [String]! 

この

var cars = [Car]() 

というより作ります

for user in users { 

      carType.append(user[Variables.rCar]) 
      rate.append(user[Variables.rRate]) 
      location.append(user[Variables.rLocation])   
     } 

この

for user in users { 
      let car = Car() 
      car.carType = user[Variables.rCar] 
      car.location = user[Variables.rLocation] 
      car.rate = user[Variables.rRate] 
      cars.append(car)  
     } 

作ると

cell.carType.text = "Car Type: " + cars[indexPath.row].carType 
    cell.rate.text = "Location: " + cars[indexPath.row].rate 
    cell.carImage.image = UIImage(named: cars[indexPath.row].carType+".jpg") 

に変更し、重要なことは、filteredCarsは車のオブジェクトの配列です

filteredCars = cars.filter{ 
$0.carType.lowercased == searchText.lowercased} 
} 

ということですcellForRow。これがあなたのお役に立てば幸いです。

+0

where filtered filters = cars.filter { $ 0.carType.lowercased == searchText.lowercased} } –

+0

はfilteredLocation = searchText.isEmptyではなくsearchBarメソッドに入れていますか? location:location.filter({(dataString:String) - > Bool in 返信dataString.range(of:searchText、options:.caseInsensitive)!= nil }) – Hosny

+0

とnumberOfRowsInSectionメソッドのcars.countを返します。 ?どうもありがとうございます! –

関連する問題