2017-10-19 65 views
0

我正在开发一个汽车租赁应用程序,该应用程序接受用户的输入并将其放入我的SQLite数据库。搜索栏刷新我的表格视图单元格的索引

在我的tableview视图控制器中,它显示的信息很好。

实施例:

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

的问题是,每当我搜索,例如我搜索“英格兰”,它显示的唯一正确的数据是只有位置。因此,它会显示如下:

Cell 1: Toyota, England, 5000

这是Toyota5000从指数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,但最初过滤的数据,但我编辑它来解释它很容易。 –

+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)  
     } 

和cellForRow其更改为

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 = cars.filter{ 
$0.carType.lowercased == searchText.lowercased} 
} 

其中filteredCars是汽车对象的数组。希望这对你有所帮助。

+0

我在哪里放了filteredCars = cars.filter { $ 0.carType.lowercased == searchText.lowercased} } –

+0

在searchBar方法而不是filteredLocation = searchText.isEmpty?位置:location.filter({(dataString:String) - > Bool in return dataString.range(of:searchText,options:.caseInsensitive)!= nil }) – Hosny

+0

and inside numberOfRowsInSection method我将返回cars.count权限?非常感谢! –