gpt4 book ai didi

ios - 搜索栏正在刷新我的表格 View 单元格的索引

转载 作者:行者123 更新时间:2023-11-30 12:04:24 24 4
gpt4 key购买 nike

我正在开发一个汽车租赁应用程序,它接受用户的输入并将其放入我的 SQLite 数据库中。

在我的 TableView View Controller 中,它显示的信息很好。

示例:

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

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

单元 1:丰田,英国,5000

Toyota5000 来自索引 0。唯一正确的数据是英格兰。

每当我搜索“england”时我想要的结果:1 号小区:英国三菱,2000 年

请帮我修复汽车类型和费率以将其也显示出来。

这是我的 TableView Controller 中的代码:

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")

}

最佳答案

您可以为它们创建对象,而不是为数据创建 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是汽车对象的数组。希望这对您有帮助。

关于ios - 搜索栏正在刷新我的表格 View 单元格的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46831485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com