gpt4 book ai didi

ios - 具有自定义类的搜索栏

转载 作者:行者123 更新时间:2023-12-01 22:19:54 31 4
gpt4 key购买 nike

我是 iOS 开发的初学者,在尝试使用 UISearchBar 时遇到问题在 UITableView .
我不知道如何在使用搜索栏过滤数据的表格 View 中使用自定义类。请问,你会解决这个问题吗?下面是自定义类:

//  Data.swift
// SearchBarWithTableView

class Food {
var FoodName:String = ""
var FoodQuantity:String = ""
var FoodGroup:String = ""
var FoodImage:String = ""
var FoodCarbs:Int = 0
var FoodCalories:Int = 0

init(foodName:String,foodGroup:String,foodQuantity:String,foodImage:String,foodCarbs:Int,foodCalories:Int) {
self.FoodName = foodName
self.FoodQuantity = foodQuantity
self.FoodGroup = foodGroup
self.FoodImage = foodImage
self.FoodCarbs = foodCarbs
self.FoodCalories = foodCalories
}}

ViewController.swift
import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{

@IBOutlet weak var SearchBarTableView:UITableView!
@IBOutlet weak var SearchBar:UISearchBar!




var searchActive:Bool=false
var filteredData=[Food]()

let allData:[Food]=[Food(foodName: "Rice", foodGroup: "Beans", foodQuantity: "5 scope",foodImage:"1S.jpg", foodCarbs:15,foodCalories: 80),
Food(foodName: "Apple", foodGroup: "Frutie", foodQuantity: "One",foodImage:"S-2.jpg", foodCarbs: 15, foodCalories: 80),
Food(foodName: "ban 1",foodGroup: "Beans",foodQuantity:"One",foodImage:"3S.jpg",foodCarbs: 15,foodCalories: 80),
Food(foodName: "ban 2", foodGroup: "Beans", foodQuantity: "half pieace",foodImage:"4-S.jpg", foodCarbs: 25, foodCalories: 140)]



override func viewDidLoad() {

super.viewDidLoad()

SearchBarTableView.delegate=self
SearchBarTableView.dataSource=self
SearchBar.delegate=self
filteredData=allData
print(filteredData.count)
print("-----------")
print(allData.count)
SearchBar.returnKeyType=UIReturnKeyType.done
definesPresentationContext=true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "cell")
var Text:String
if searchActive {
Text=filteredData[indexPath.row].FoodName
}else{
Text=allData[indexPath.row].FoodName
}
cell?.textLabel?.text=filteredData[indexPath.row].FoodName
return cell!
}

func searchBar(_ searchBar: UISearchBar, textDidChange `searchText: String) {`
if SearchBar.text == nil || SearchBar.text == "" {
searchActive=false
view.endEditing(true)
SearchBarTableView.reloadData()
}else{
searchActive=true
/* here is the problem */
// filteredData=allData.filter({$0 === searchBar.text!})
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if searchActive {
return filteredData.count
}
return allData.count
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier=="Go"{
if let indexPath=SearchBarTableView.indexPathForSelectedRow{
let destinationVC=segue.destination as! DetailsViewController
destinationVC.namE=allData[indexPath.row].FoodName
destinationVC.quntitY=allData[indexPath.row].FoodQuantity
destinationVC.calorieS=allData[indexPath.row].FoodCalories
destinationVC.carbS=allData[indexPath.row].FoodCarbs
}
}
}
}

最佳答案

    func searchBar(_ searchBar: UISearchBar, textDidChange `searchText: String) {`
if SearchBar.text == nil || SearchBar.text == "" {
searchActive=false
view.endEditing(true)
SearchBarTableView.reloadData()
}else{
searchActive=true

filteredData=allData.filter({$0.FoodName === searchBar.text!})
SearchBarTableView.reloadData()


}
}

关于ios - 具有自定义类的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45460724/

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