Ios 如何在swift 3.1.1中为搜索栏使用getter setter方法?
我已经在我的应用程序中实现了搜索栏。我有一些酒店的JSON数据。(姓名、图像、地址、手机号码、电子邮件)。我将在搜索结果中获取名称,但我希望获取特定酒店的全部数据。所以,请帮助我实现搜索功能。我想使用getter setter方法。请帮忙。提前谢谢Ios 如何在swift 3.1.1中为搜索栏使用getter setter方法?,ios,swift3,uisearchbar,getter-setter,Ios,Swift3,Uisearchbar,Getter Setter,我已经在我的应用程序中实现了搜索栏。我有一些酒店的JSON数据。(姓名、图像、地址、手机号码、电子邮件)。我将在搜索结果中获取名称,但我希望获取特定酒店的全部数据。所以,请帮助我实现搜索功能。我想使用getter setter方法。请帮忙。提前谢谢 let str = self.catArr [indexPath.row] let url1 = "myapi" let url2 = url1+str let allowedCharacterSet = (Characte
let str = self.catArr [indexPath.row]
let url1 = "myapi"
let url2 = url1+str
let allowedCharacterSet = (CharacterSet(charactersIn: " ").inverted)
if let url = url2.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)
{
Alamofire.request(url).responseJSON { (responseData) -> Void in
if (responseData.result.value) == nil
{
print("Error!!")
}
//if (responseData.result.value) != nil
else
{
let response = JSON(responseData.result.value!)
if let resData = response["data"].arrayObject
{
self.arrResponse1 = resData as! [[String: AnyObject]]
for item in self.arrResponse1
{
let name = item["name"] as! String
self.arrName.append(name)
let add = item["address"] as! String
self.arrAddress.append(add)
let web = item["website"] as! String
self.arrWebsite.append(web)
let email = item["email"] as! String
self.arrEmail.append(email)
let mob = item["mobile"] as! String
self.arrMobile.append(mob)
let city = item["city"] as! String
self.arrCity.append(city)
let state = item["state"] as! String
self.arrState.append(state)
let dist = item["district"] as! String
self.arrDistrict.append(dist)
let area = item["area"] as! String
self.arrArea.append(area)
let img = item["image"] as! String
self.arrImage.append(img)
let rating = item["rating"] as! String
self.arrRating.append(rating)
let id = item["id"] as! String
self.arrId.append(id)
}
}
}
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
filteredName = self.nameArr.filter({ (String) -> Bool in
let tmp: NSString = String as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(filteredName.count == 0)
{
searchActive = false;
}
else
{
searchActive = true;
print("Search Array = \(filteredName)")
}
self.infoTableView.reloadData()
}
//在tableview中显示
如果(搜索活动)
{
cell.lblName.text=self.filteredName[indexPath.row]
}
else
{
let urlImage = str1+self.imageArr [indexPath.row]+".jpg"
cell.imgView.sd_setImage(with: URL(string: urlImage), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblAddress.text = self.addressArr [indexPath.row]
cell.lblCity.text = self.cityArr [indexPath.row]
cell.lblPhone.text = self.mobileArr [indexPath.row]
cell.lblEmail.text = self.emailArr [indexPath.row]
cell.lblStar.text = self.ratingArr [indexPath.row]
cell.phoneString = self.mobileArr [indexPath.row]
cell.emailString = self.emailArr [indexPath.row]
}
与使用多个数组来存储每个值不同,您需要的是自定义
类/结构
对象的数组,然后它就可以方便地使用过滤器
struct Item { //Make some suitable name
let id: String
let name: String
let address: String
let website: String
let email: String
let mobile: String
let city: String
let state: String
let district: String
let area: String
let image: String
let rating: String
init(dictionary: [String:Any]) {
self.id = dictionary["id"] as? String ?? "Default Id"
self.name = dictionary["name"] as? String ?? "Default name"
self.address = dictionary["address"] as? String ?? "Default address"
self.website = dictionary["website"] as? String ?? "Default website"
self.email = dictionary["email"] as? String ?? "Default email"
self.mobile = dictionary["mobile"] as? String ?? "Default mobile"
self.city = dictionary["city"] as? String ?? "Default city"
self.state = dictionary["state"] as? String ?? "Default state"
self.district = dictionary["district"] as? String ?? "Default district"
self.area = dictionary["area"] as? String ?? "Default area"
self.image = dictionary["image"] as? String ?? "Default image"
self.rating = dictionary["rating"] as? String ?? "Default rating"
}
}
现在您只需要两个[Item]
类型的数组,一个用于普通数据,另一个用于过滤器
数据,所以像这样声明两个数组
var items = [Item]()
var filterItems = [Item]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return filterItems.count
}
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! YourCustomCell
let item = searchActive ? filterItems[indexPath.row] : items[indexPath.row]
cell.lblName.text = item.name
cell.lblAddress.text = item.address
//...Set others detail same way
return cell
}
在您的Alamofire
请求中,以这种方式初始化此项
数组
let response = JSON(responseData.result.value!)
if let resData = response["data"].arrayObject as? [[String: Any]] {
self.items = resData.map(Item.init)
}
现在在tableView
方法中使用这两个数组,如下所示
var items = [Item]()
var filterItems = [Item]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return filterItems.count
}
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! YourCustomCell
let item = searchActive ? filterItems[indexPath.row] : items[indexPath.row]
cell.lblName.text = item.name
cell.lblAddress.text = item.address
//...Set others detail same way
return cell
}
现在,使用textDidChange
以这种方式过滤项
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
filterItems = self.items.filter({
$0.name.localizedCaseInsensitiveContains(searchText)
})
if(filterItems.count == 0) {
searchActive = false
}
else {
searchActive = true
print("Search Array = \(filterItems)")
}
self.infoTableView.reloadData()
}
你能告诉代码你是如何得到这个JSON``数据的,以及你是如何在
tableView`@NiravD:::使用alamofire,我从JSON得到响应,我将它存储在不同的数组中,并在tableView的标签中按行打印它们。self.nameArr是名称数组。你能展示一下你的一些示例吗?这会让我们知道你正在从JSON
@NiravD Alamofire.request(url).responseJSON{response in if let JSON=response.result.value访问什么密钥{let responseRes=JSON as?如果let resData=responseRes?[“data”]as?[AnyObject]{self.arrResponse=resData as![[String:AnyObject]]用于self.arrResponse{et name=item[“name”]as!String self.arrName.append(name)}}}}不要在注释中添加代码编辑您的问题并用它编码一切正常。谢谢您的帮助。!!编码愉快。@A21欢迎伴侣:)