I have a UITablView
contains number of cells that represent employees for each.
And I have UISearchBar
to filter the list of employees, I'm storing employee data as employee object which has number of properties so I'm filtering the employees.
What I couldn't filter is the employee photo because I didn't save it as property in employee object , I call function for downloading the image inside cellForRowAtIndexPath
and that's it I can't use the image again .
I need a way to save the image for 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 as 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 as 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 is 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 is 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 and add it to the searcing array
for i 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 my way for getting the images is wrong because in my case I need to save the images for later use and I'm not also I couldn't find a way to async download image then after loading it save it in array or something.
Any help ?