以下代码成功创建,在Cars
中显示UITableView
的列表,您以后也可以删除购物车。它还显示一个UISearchController
,您可以在其中成功执行搜索。
例如,如果用户搜索位于数组中间的汽车,它会在表格的第一行显示正确的汽车但是如果他/她决定删除它,则它将删除cars
数组中的第一项,因为过滤后的项始终位于filteredCars
数组的顶部。在这里我没有收到任何错误,但是它不会从cars
数组中删除正确的项目,而是总是从cars
数组中删除第一项目。
这里是代码:
class Car{
var make = ""
var model = ""
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating{
var cars = Array<Car>()
var filteredCars = Array<Car>()
let searchController = UISearchController(searchResultsController: nil)
@IBOutlet weak var myTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
createCars()
filteredCars = cars
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search by model"
navigationItem.searchController = searchController
definesPresentationContext = true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredCars.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell")! as UITableViewCell
cell.textLabel?.text = filteredCars[indexPath.row].model
cell.detailTextLabel?.text = filteredCars[indexPath.row].make
return cell
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { action, index in
let alert = UIAlertController(title: "Delete selected car?", message: "This will permanently delete the selected car, do you want to continue?", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.destructive, handler: { action in
self.filteredCars.remove(at: indexPath.row)
self.cars.remove(at: indexPath.row)
self.myTable.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
}
))
self.present(alert, animated: true, completion: nil)
}
return [delete]
}
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
filteredCars = searchText.isEmpty ? cars : cars.filter({(dataString: Car) -> Bool in
return dataString.model.lowercased().contains(searchText.lowercased())
})
myTable.reloadData()
}
}
// create cars manually for demonstration only
func createCars(){
let car1 = Car()
car1.make = "Ford"
car1.model = "Explorer"
//... more cars here
cars.append(contentsOf: [car1, car2, car3, car4])
}
}
我尝试了以下操作,但仍然出现Index out of range
错误。
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { action, index in
let alert = UIAlertController(title: "Delete selected car?", message: "This will permanently delete the selected car, do you want to continue?", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.destructive, handler: { action in
self.filteredCars.remove(at: indexPath.row)
self.myTable.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
for i in 0..<self.cars.count {
if self.cars[i].model == modelToDelete{
self.cars.remove(at:i)
}
}
}
))
self.present(alert, animated: true, completion: nil)
}
return [delete]
}
搜索后删除项目的正确逻辑是什么?
您必须在给定汽车的主数组中获得索引
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.destructive, handler: { action in
let carToDelete = self.filteredCars.remove(at: indexPath.row)
self.cars.remove(at: self.cars.index(of: carToDelete)!)
self.myTable.deleteRows(at: [indexPath], with: .left)
}
这要求Car
采用Equatable