2016-09-22 48 views
0

我有一個UITablView包含代表每個員工的單元數。如何過濾包含異步下載圖像的UITableviewcells - Swift

而且我有UISearchBar來過濾員工列表,我將員工數據存儲爲具有多個屬性的員工對象,所以我正在過濾員工。

我無法過濾的是員工照片,因爲我沒有將其作爲員工對象中的屬性保存,所以我調用下載cellForRowAtIndexPath中的圖像的功能,就是這樣我無法再使用該圖像。

我需要一種方法來保存圖像供以後使用。

cellForRowAtIndexPath代碼:

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 

} 

這被下載圖像功能:

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() 
} 

}

這是searchBar功能:

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() 
    } 

} 

我想我的方式獲取圖像是錯誤的,因爲在我的情況下,我需要保存圖像供以後使用,我也不是我找不到異步下載圖像的方式,然後加載它保存後它在數組或其他東西。

任何幫助?

+0

您可以保存成功下載後,文檔目錄,保持路徑或最後圖像路徑的名稱(其他路徑對於所有文檔目錄都是相同的)。然後使用該屬性從文檔目錄中獲取圖像。那有意義嗎 ? – Janmenjaya

回答

2

考慮使用SDWebImage庫下載並緩存圖像,這樣每次調用cellForRowAtIndexPath時都不會下載它們,也可以解決您的問題。

在你的cellForRowAtIndexPath只需使用

cell.empPhoto.sd_setImageWithURL(self.imageURL) 

希望這會有所幫助。

+0

謝謝,但對不起,我是新來的這個文件應該複製到我的項目? – Zizoo

+0

從此鏈接下載zip。在zip裏面你會找到一個名爲SDWebImage的文件夾,將文件夾和其中包含的所有文件添加到你的項目中,然後將這個導入添加到你的橋接頭#import ,你很好。 –

+0

如果您在爲項目添加橋接頭時需要幫助,請參閱此鏈接http://stackoverflow.com/questions/31716413/xcode-not-automatically-creating-bridging-header –

0

對於斯威夫特,有一個圖書館Kingfisher

方法是相當聰明:

import Kingfisher 
... 

let url = URL(string: "url_of_your_image") 
imageView.kf.setImage(with: url) 

您可以通過迦太基經理或手動安裝它,通過斯威夫特套餐:

https://github.com/onevcat/Kingfisher/wiki/Installation-Guide

我喜歡Pods方式:

source 'https://github.com/CocoaPods/Specs.git' 
platform :ios, '10.0' 
use_frameworks! 

target 'MyApp' do 
    # your other pod 
    # ... 
    pod 'Kingfisher', '~> 3.0' 
end 

更多的文檔是在這裏:

http://cocoadocs.org/docsets/Kingfisher/3.1.0/

https://github.com/onevcat/Kingfisher