如何在 swift 3.1.1 中使用 getter setter 搜索栏的方法?
how to use getter setter method for search bar in swift 3.1.1?
我在我的应用程序中实现了搜索栏。我有一些酒店的 JSON 数据。 (姓名、照片、地址、手机号码、电子邮件)。我正在搜索结果中获取名称,但我想获取特定酒店的全部数据。所以,请帮我实现搜索功能。我想使用 getter setter 方法。请帮忙。提前致谢!!
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()
}
//在表格视图中显示
如果(搜索活动)
{
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]
}
不需要多个数组来存储每个值,您需要的是自定义 class/struct
对象数组,然后它易于使用 filter
。
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]
类型的数组,一个用于普通数据,一个用于 filter
数据,所以像这样声明两个数组。
var items = [Item]()
var filterItems = [Item]()
在您的 Alamofire
请求中以这种方式初始化了这个 items
数组。
let response = JSON(responseData.result.value!)
if let resData = response["data"].arrayObject as? [[String: Any]] {
self.items = resData.map(Item.init)
}
现在像这样在 tableView
方法中使用这两个数组。
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
以这种方式过滤您的 items
数组。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
filterItems = self.items.filter({
[=14=].name.localizedCaseInsensitiveContains(searchText)
})
if(filterItems.count == 0) {
searchActive = false
}
else {
searchActive = true
print("Search Array = \(filterItems)")
}
self.infoTableView.reloadData()
}
我在我的应用程序中实现了搜索栏。我有一些酒店的 JSON 数据。 (姓名、照片、地址、手机号码、电子邮件)。我正在搜索结果中获取名称,但我想获取特定酒店的全部数据。所以,请帮我实现搜索功能。我想使用 getter setter 方法。请帮忙。提前致谢!!
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()
}
//在表格视图中显示 如果(搜索活动) { 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]
}
不需要多个数组来存储每个值,您需要的是自定义 class/struct
对象数组,然后它易于使用 filter
。
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]
类型的数组,一个用于普通数据,一个用于 filter
数据,所以像这样声明两个数组。
var items = [Item]()
var filterItems = [Item]()
在您的 Alamofire
请求中以这种方式初始化了这个 items
数组。
let response = JSON(responseData.result.value!)
if let resData = response["data"].arrayObject as? [[String: Any]] {
self.items = resData.map(Item.init)
}
现在像这样在 tableView
方法中使用这两个数组。
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
以这种方式过滤您的 items
数组。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
filterItems = self.items.filter({
[=14=].name.localizedCaseInsensitiveContains(searchText)
})
if(filterItems.count == 0) {
searchActive = false
}
else {
searchActive = true
print("Search Array = \(filterItems)")
}
self.infoTableView.reloadData()
}