2017-08-01 56 views
0

我想弄清楚如何篩選數據並重新加載tableview。Swift 3:使用ID和重新加載tableview篩選結果

我有4個地區(北,南,東,西)和所有。

默認情況下,它將顯示所有配置文件。

如果我按北(即1),它應該只過濾在北方地區

同樣可用配置文件,與南部,東部和西部。根據按下的區域,它應該過濾配置文件。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.section == 0 
    { 
     let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell", for: indexPath) as! BussinessCell 

     let tableObjects:StorageSubCatagoriezTwo = subCatagorizeDataTwo[indexPath.row] 

     let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "") 
     cell.bussinessWebView.scrollView.isScrollEnabled = false 

     var strings = "" 

     if regId == 1 
     { 
      cell.bussinessName.text = " " 
      let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row] 

      if tableObjects.address.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "") 
      } 
      else 
      { 
       //nothing 
      } 
      if tableObjects.telephone.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.email.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.website.characters.isEmpty != true{ 
       strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "") 
      } 
      else{ 

      } 
     } 
     else if regId == 2 
     { 
      cell.bussinessName.text = " " 
      let tableObjects:StorageSubCatagoriezTwoEast = subCatagorizeDataTwoE[indexPath.row] 
      //cell.bussinessName.text = tableObjects.nameString 
      if tableObjects.address.characters.isEmpty != true 
      { 
       // cell.bussinessName.text = tableObjects.nameString 
       strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "") 
      } 
      else 
      { 
       //nothing 
      } 
      if tableObjects.telephone.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.email.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.website.characters.isEmpty != true{ 
       strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "") 
      } 
      else{ 

      } 
     } 
     else if regId == 3 
     { 
      cell.bussinessName.text = " " 
      let tableObjects:StorageSubCatagoriezTwoSouth = subCatagorizeDataTwoS[indexPath.row] 
      // cell.bussinessName.text = tableObjects.nameString 
      if tableObjects.address.characters.isEmpty != true 
      { 
       //cell.bussinessName.text = tableObjects.nameString 
       strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "") 
      } 
      else 
      { 
       //nothing 
      } 
      if tableObjects.telephone.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.email.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.website.characters.isEmpty != true{ 
       strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "") 
      } 
      else{ 

      } 
     } 
     else if regId == 4 
     { 
      cell.bussinessName.text = " " 
      let tableObjects:StorageSubCatagoriezTwoWest = subCatagorizeDataTwoW[indexPath.row] 
     // cell.bussinessName.text = tableObjects.nameString 
      if tableObjects.address.characters.isEmpty != true 
      { 
       // cell.bussinessName.text = tableObjects.nameString 
       strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "") 
      } 
      else 
      { 
       //nothing 
      } 
      if tableObjects.telephone.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.email.characters.isEmpty != true 
      { 
       strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "") 
      } 
      else 
      { 

      } 
      if tableObjects.website.characters.isEmpty != true{ 
       strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "") 
      } 

     } 
     else{ 
      let tableObjects:StorageSubCatagoriezTwo = subCatagorizeDataTwo[indexPath.row] 
      cell.bussinessName.text = tableObjects.nameString 

      let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "") 
      print(charCheck) 
      cell.bussinessName.text = " " 

      var strings = "" 

      strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "") 
      strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.address+"</b></font><br>" : "") 
      // strings = strings+(tableObjects.address.characters.count > 0 ? "<img src=\"http://getmydetails.pe.hu/img/yt.jpg\"> "+tableObjects.address+"<br>" : "") 

      strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "") 

      strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/email.gif\"> "+tableObjects.email+"<br>" : "") 

      strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "") 

      // cell.bussinessWebView.loadHTMLString("<font size=\"5\"><b>"+tableObjects.nameString+"</b></font><br><img src=\"http://getmydetails.pe.hu/img/yt.jpg\"> "+tableObjects.address+"<br><img src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br><img src=\"http://getmydetails.pe.hu/img/email.gif\"> "+tableObjects.email+"<br><img src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website, baseURL: nil) 

      cell.bussinessWebView.loadHTMLString(strings, baseURL: nil) 
      let heightIndex = strings.components(separatedBy: "<br>") 
      let heightMul = heightIndex.count 
      print(heightMul*32) 

      self.heightOfTheCell = CGFloat(((heightMul)*32)+4) 


     } 
     return cell 
    } 
    else if indexPath.section == 1 
    { 
     let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell2") 
     cell?.textLabel?.textAlignment = .center 
     switch regId 
     { 
     case 0: 
      if subCatagorizeDataTwo.count != forAll 
      { 
       cell?.textLabel?.text = "LOAD MORE DATA" 
       cell?.textLabel?.textColor = Color.whiteColor 
       cell?.backgroundColor = UIColor.darkGray 

      }else{ 
       cell?.textLabel?.text = "NO MORE FOUND!" 
       cell?.textLabel?.textColor = UIColor.darkGray 
       cell?.backgroundColor = UIColor.gray 
       cell?.selectionStyle = UITableViewCellSelectionStyle.none 
      } 
     case 1: 
      if subCatagorizeDataTwoN.count != forN 
      { 
       cell?.textLabel?.text = "LOAD MORE DATA" 
       cell?.textLabel?.textColor = Color.whiteColor 
       cell?.backgroundColor = UIColor.darkGray 

      }else{ 
       cell?.textLabel?.text = "NO MORE FOUND!" 
       cell?.textLabel?.textColor = UIColor.darkGray 
       cell?.backgroundColor = UIColor.gray 
       cell?.selectionStyle = UITableViewCellSelectionStyle.none 
      } 
     case 2: 
      if subCatagorizeDataTwoE.count != forE 
      { 
       cell?.textLabel?.text = "LOAD MORE DATA" 
       cell?.textLabel?.textColor = Color.whiteColor 
       cell?.backgroundColor = UIColor.darkGray 

      }else{ 
       cell?.textLabel?.text = "NO MORE FOUND!" 
       cell?.textLabel?.textColor = UIColor.darkGray 
       cell?.backgroundColor = UIColor.gray 
       cell?.selectionStyle = UITableViewCellSelectionStyle.none 
      } 
     case 3: 
      if subCatagorizeDataTwoS.count != forS 
      { 
       cell?.textLabel?.text = "LOAD MORE DATA" 
       cell?.textLabel?.textColor = Color.whiteColor 
       cell?.backgroundColor = UIColor.darkGray 

      }else{ 
       cell?.textLabel?.text = "NO MORE FOUND!" 
       cell?.textLabel?.textColor = UIColor.darkGray 
       cell?.backgroundColor = UIColor.gray 
       cell?.selectionStyle = UITableViewCellSelectionStyle.none 
      } 
     case 4: 
      if subCatagorizeDataTwoW.count != forW 
      { 
       cell?.textLabel?.text = "LOAD MORE DATA" 
       cell?.textLabel?.textColor = Color.whiteColor 
       cell?.backgroundColor = UIColor.darkGray 

      }else{ 
       cell?.textLabel?.text = "NO MORE FOUND!" 
       cell?.textLabel?.textColor = UIColor.darkGray 
       cell?.backgroundColor = UIColor.gray 
       cell?.selectionStyle = UITableViewCellSelectionStyle.none 
      } 

     default:break 
     } 

     return cell! 
    } 
    else 
    { 
     let tableObjects:StorageSubCatagoriez3rd = SubCatagoriez3rd[indexPath.row] 
     let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell3") 
     cell?.textLabel?.text = tableObjects.nameString 
     return cell! 

    } 
} 

請看截圖:

When Displaying all

When Displaying South Region

而且,在第二截圖(南區)看到的輪廓是爲了隱藏,因爲它屬於北地區。

請讓我知道我該如何解決這個問題。

感謝您的時間:)

型號:

class StorageSubCatagoriezTwo 
{ 
var nameString:String 
var idString:String 
var address:String 
var telephone:String 
var email:String 
var website:String 

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String) 
{ 
    self.nameString = tempName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.idString = tempID.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.address = tempaddress.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.telephone = tempTelephone.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.email = tempEmail.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.website = tempWebsite.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
} 
} 

    class StorageSubCatagoriezTwoNorth 

{ 
var nameString:String 
var idString:String 
var address:String 
var telephone:String 
var email:String 
var website:String 

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String) 
    { 
    self.nameString = tempName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.idString = tempID.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.address = tempaddress.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.telephone = tempTelephone.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.email = tempEmail.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.website = tempWebsite.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
} 
} 

class StorageSubCatagoriezTwoEast 
{ 
var nameString:String 
var idString:String 
var address:String 
var telephone:String 
var email:String 
var website:String 

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String) 
{ 
    self.nameString = tempName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.idString = tempID.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.address = tempaddress.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.telephone = tempTelephone.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.email = tempEmail.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.website = tempWebsite.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
} 
} 

class StorageSubCatagoriezTwoWest 
{ 
var nameString:String 
var idString:String 
var address:String 
var telephone:String 
var email:String 
var website:String 

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String) 
{ 
    self.nameString = tempName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.idString = tempID.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.address = tempaddress.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.telephone = tempTelephone.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.email = tempEmail.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.website = tempWebsite.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } 
} 

class StorageSubCatagoriezTwoSouth 
{ 
var nameString:String 
var idString:String 
var address:String 
var telephone:String 
var email:String 
var website:String 

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String) 
{ 
    self.nameString = tempName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.idString = tempID.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.address = tempaddress.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.telephone = tempTelephone.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.email = tempEmail.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    self.website = tempWebsite.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
} 
} 

取出區域和分配到變量:

let id = String(describing: dictionary["id"]!) 
         let name = String(describing: dictionary["BUSINESS_NAME"]!) 
         let email = String(describing: dictionary["EMAIL"]!) 
         var streetNo = String(describing: dictionary["STREET_NO"]!) 
         var streetName = String(describing: dictionary["STREET_NAME"]!) 
         var suburbID = String(describing: dictionary["SUBURB_ID"]!) 
         var postCode = String(describing: dictionary["POSTCODE"]!) 
         let regionID = String(describing: dictionary["REGION_ID"]!) 
         var landline = String(describing: dictionary["PHONE_FIXED"]!) 
         var mobile = String(describing: dictionary["PHONE_MOBILE"]!) 
         let website = String(describing: dictionary["WEBURL"]!) 
         var suburbName = String(describing: dictionary["Suburb"]!) 
         let streedNoHide = String(describing: dictionary["STREET_NO_HIDDEN"]!) 
         let streetNameHide = String(describing: dictionary["STREET_NAME_HIDDEN"]!) 
         let suburbHide = String(describing: dictionary["SUBURB_HIDDEN"]!) 
         let postCodeHide = String(describing: dictionary["POSTCODE_HIDDEN"]!) 
         let mobileHide = String(describing: dictionary["PHONE_MOBILE_HIDDEN"]!) 
         let landlineHide = String(describing: dictionary["PHONE_FIXED_HIDDEN"]!) 

JSON數據:(REGION_ID)

enter image description here

+0

你可以添加你的類別模型嗎? – Jaydeep

+0

如何識別哪個配置文件屬於哪個區域? – Surjeet

+0

代碼中的哪個位置可以設置regId的值? –

回答

0

1)他們應該是2個不同的陣列。

var mainArray = [StorageSubCatagories]() 
var filteredArray = [StorageSubCatagories]() 

第一個數組具有所有對象。(mainArray)

第二個數組只有主數組的過濾對象。(filteredArray)

2)您應該使用普通類,如 'StorageSubCatagories'分析所有的數據,而不是使用像StorageSubCatagoriezTwo,StorageSubCatagoriezTwoNorth,StorageSubCatagoriezTwoEast ...等多個類,存儲中的所有對象成一個單一的陣列象下面這樣:

3) 該函數將返回根據按鈕選擇濾波的數據。 對於按鈕全部通過regionId = 0,對於北部regionId = 1,....根據您的值。

func fetchResultForSelection(_ regionId: Int) -> [StorageSubCatagories] { 
     if regionId == 0 { 
      return mainArray 
     } 
     let tempArray = mainArray.filter { $0.regId == regionId } 
     return tempArray 
    } 

刪除先前的對象後,將從此函數返回的數組對象添加到filteredArray中。

4)始終根據filteredArray而不是mainArray顯示您的表數據。

希望這對你有所幫助。

+0

謝謝Surjeet。我會試試這個。讓我們看看 –

+0

嘿,我試過了你推薦的解決方案,但顯然它還沒有工作。 –

+0

你從這個func fetchResultForSelection收到了多少個對象? – Surjeet