ios - How to filter UITableviewcells that contains async downloaded images - Swift -
i have uitablview
contains number of cells represent employees each.
and have uisearchbar
filter list of employees, i'm storing employee data employee object has number of properties i'm filtering employees.
what couldn't filter employee photo because didn't save property in employee object , call function downloading image inside cellforrowatindexpath
, that's can't use image again .
i need way save image later use .
this cellforrowatindexpath
code :
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = self.tableview.dequeuereusablecell(withidentifier: cellid, for: indexpath) as! employeecell if self.issearching == true { let emp = searchedemployees[(indexpath nsindexpath).row] cell.empname.text = emp.getfistname() + " " + emp.getlastname() cell.empposition.text = emp.getposition() cell.status.text=emp.getstatus() let myurlstring=emp.getphotourl()! let myurl = myurlstring.addingpercentencoding( withallowedcharacters: characterset.urlqueryallowed) cell.empphoto.downloadimagefrom(link: myurl!, contentmode: uiviewcontentmode.scaleaspectfit) } else{ let emp = employees[(indexpath nsindexpath).row] cell.empname.text = emp.getfistname() + " " + emp.getlastname() cell.empposition.text = emp.getposition() cell.status.text=emp.getstatus() let myurlstring=emp.getphotourl()! let myurl = myurlstring.addingpercentencoding( withallowedcharacters: characterset.urlqueryallowed) cell.empphoto.downloadimagefrom(link: myurl!, contentmode: uiviewcontentmode.scaleaspectfit) } return cell }
this downloading image function :
extension uiimageview { func downloadimagefrom(link:string, contentmode: uiviewcontentmode){ urlsession.shared.datatask( with: url(string:link)!, completionhandler: { (data, response, error) -> void in dispatchqueue.main.async { self.contentmode = contentmode if let data = data { self.image = uiimage(data: data) self.image = self.image?.circle self.image = self.image?.rounded } } }).resume() }
}
this searchbar
function :
func searchbar(_ searchbar: uisearchbar, textdidchange searchtext: string) { if self.searchbar.text!.isempty { // set searching false self.issearching = false // reload table view self.tableview.reloaddata() }else{ // set searghing true self.issearching = true // empty searching array self.searchedemployees.removeall(keepingcapacity: false) // find matching item , add searcing array in 0..<self.employees.count { let firstname : string = self.employees[i].getfistname() let lastname : string = self.employees[i].getlastname() let fullname : string = self.employees[i].getfistname()+" "+self.employees[i].getlastname() if firstname.lowercased().range(of: self.searchbar.text!.lowercased()) != nil || lastname.lowercased().range(of: self.searchbar.text!.lowercased()) != nil || fullname.lowercased().range(of: self.searchbar.text!.lowercased()) != nil{ self.searchedemployees.append(self.employees[i]) } } self.tableview.reloaddata() } }
i think way getting images wrong because in case need save images later use , i'm not couldn't find way async download image after loading save in array or something.
any ?
consider using sdwebimage library downloads , caches images not downloaded every time cellforrowatindexpath called , solve problem.
just use
cell.empphoto.sd_setimagewithurl(self.imageurl)
in cellforrowatindexpath.
hope helps.
Comments
Post a Comment