2017-06-16 71 views
0

我在我的應用程序中實現了搜索欄。我有一些酒店的JSON數據。 (姓名,圖片,地址,手機號碼,電子郵件)。我在搜索結果中獲得了該名稱,但我想要獲取特定酒店的全部數據。所以,請幫助我實現搜索功能。我想使用getter setter方法。請幫忙。提前致謝!!如何在swift 3.1.1中使用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() 
} 

//在tableview中 表示如果(searchActive) { 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] 
    } 
+0

你能告訴你如何得到這個'JSON''數據,以及你如何在'tableView'顯示代碼 –

+0

@NiravD :::使用alamofire,我正在從JSON響應,我將其存儲在不同的數組中,並在tableview中將它們打印在標籤中。 self.nameArr是名稱數組。 – A21

+0

你可以向我們展示你的一些樣本'JSON',它可以讓我們知道你從'JSON'訪問什麼樣的密鑰 –

回答

0

具有多個數組來存儲代替每個值你需要的是自定義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({ 
     $0.name.localizedCaseInsensitiveContains(searchText) 
    }) 

    if(filterItems.count == 0) { 
     searchActive = false 
    }    
    else { 
     searchActive = true 
     print("Search Array = \(filterItems)") 
    }   
    self.infoTableView.reloadData() 
} 
+0

一切正常精細。謝謝你的幫助。!! 快樂編碼。 – A21

+0

@ A21歡迎伴侶:) –