ios - Swift SearchBar Filtering & Updating Multiple Arrays -


i need implement searchbar searches & filters tableview 2 labels. label data coming 2 different arrays. when filter through array 1/label 1 filters label 2 remains same results mixed. both arrays created after sql query results 2 columns of data. mean arr1[0] , arr2[0] same row different columns. i'm stuck after many tries. here's latest code:

var arr1 = [string]() var arr2 = [string]() var filtered:[string] = []  override func numberofsections(in tableview: uitableview) -> int {     return 1 }  override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {      if(searchactive) {         return filtered.count     } else {         return arr1.count     } }   override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> xxtableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "xxcell", for: indexpath) as! xxtableviewcell      if(searchactive){          cell.label1.text = filtered[(indexpath nsindexpath).row]      } else {          cell.label1.text = arr1[(indexpath nsindexpath).row]         cell.label2.text = arr2[(indexpath nsindexpath).row]      }     return cell }  func searchbar(_ searchbar: uisearchbar, textdidchange searchtext: string) {      filtered = arr1.filter({ (text) -> bool in         let tmp: nsstring = text nsstring         let range = tmp.range(of: searchtext, options: nsstring.compareoptions.caseinsensitive)         return range.location != nsnotfound     })     if(filtered.count == 0){         searchactive = false;     } else {         searchactive = true;     }       self.tableview.reloaddata() } 

if arr1[] , arr2[] 2 columns of single row of data, should have single array. there many ways of doing - tuples, classes, structs - tend go struct. if have additional processing want perform better implemented class, same principle applies.

define struct need

struct mydatastruct {     var label1 : string = ""     var label2 : string = "" } 

then define array of type (instead of arr1, arr2)

var mydata = [mydatastruct]() 

then build data , search array before - single structure

mydata.append(mydatastruct(label1: "hello", label2: "world")) 

final step in tableview methods

override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> xxtableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "xxcell", for: indexpath) as! xxtableviewcell      if(searchactive){         cell.label1.text = filtered[indexpath.row].label1     } else {         cell.label1.text = mydata[indexpath.row].label1         cell.label2.text = mydata[indexpath.row].label2      }     return cell } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -