swift - Filtering arrays for use with UISearchBar -


i have table view displays user's name, company name , photo (pffile). each tableview row have has of information in it.

i using uisearchbardelegate , ib implement search function filter user's name. finding correct user have not been able update company photo.

how filter other arrays? items need arrays @ same index ones taken user's name array.

edit: trying different data structure , receiving array index out of range, updated code below:

var filterarray = [user]()  //<-- globally declared var userarray = [user]() //< global   class user {  var name: string? var company: string?  init (name: string?, company: string?) {      self.name = name     self.company = company     } } //in class populates search arrays object in unwrappedsucceeded {     let username = object.valueforkey("username") as! string    let companyname = object.valueforkey("companyname") as! string    let user = user(name: username, company: companyname)      userarray.append(user) }  //tableviewcontroller  func searchbar(searchbar: uisearchbar, textdidchange searchtext: string) {      filterarray.removeall(keepcapacity: false)      if searchtext.characters.count != 0 {         issearch = true          self.search(searchtext)      } else {         issearch = false     } }   func search(text: string) -> void {      filterarray = userarray.filter({$0.name == text})  } //in cellforrowatindexpath             cell.usernamecell.text = filterarray[indexpath.row].name //array index out of range 

like said recommend group each user's info 1 big container, therefore use array of struct or class, comes easier filter.

schematic container:

struct container {   var username:string?   var companyname:string?   var photo:uiimage? } 

your main array : var arrayofdata = [container]()

now when query objects parse, inside of query function

// after called findobjectswithbackgroundblock() // let's assume check error , if [pfobject] empty or not    1 in objectsfromparse  {   let phototoget = one["photo"] as! pffile  // next step should image data right :)   {     // let's assume block when image data right:)     // check data , assign uiimage      //       let userrepresentation = container()  //<-- creating single object representation each user        let username = one["username"] as! string    //<--data got parse      let companyname = one["companyname"] as! string      let userimage = //the uiimage contains data        userrepresentation.username = username      userrepresentation.companyname = companyname      userrepresentation.photo = userimage    // append      arrayofdata.append(userrepresentation)  } } 

now have data our array, let's filter username , hope configure tableview when have data filter or regular array.

  var filterarray = [container]()  //<-- globally declared    func search(text: string) -> void  {     filterarray = arrayofdata.filter(){ (container) -> bool in        let range = container.name!.rangeofstring(text,   options:nsstringcompareoptions.caseinsensitivesearch) return range != nil }        // go   } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -