ios - 如何过滤包含异步下载图像的 UITableviewcells - Swift

标签 ios iphone swift uitableview

我有一个 UITablView 包含代表每个员工的单元格数量。

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

}

我认为我获取图像的方式是错误的,因为在我的情况下,我需要保存图像供以后使用,但我不是,我也找不到异步下载图像的方法,然后在加载图像后将其保存在数组什么的。

有什么帮助吗?

最佳答案

考虑使用 SDWebImage它会下载并缓存您的图像的库,这样它们就不会在每次调用您的 cellForRowAtIndexPath 时都被下载,并且还会解决您的问题。

就用

cell.empPhoto.sd_setImageWithURL(self.imageURL)

在您的 cellForRowAtIndexPath 中。

希望这对您有所帮助。

关于ios - 如何过滤包含异步下载图像的 UITableviewcells - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39639545/

相关文章:

ios - float 和 double 有什么区别?

ios - Swift 2.3,如何向Instabug报错?

ios - 禁用约束 Storyboard

iphone - MapKit map View :viewForAnnotation has no effect on pin color

iphone - 如何在 Iphone 中调用 Magento API 登录方法

ios - 如何在 iOS 6 中从 UIScrollView 中删除 UIImageView

swift - 为什么具有可选关联值的 Swift 枚举在声明时需要尾随括号?

ios - 在 CoreData 中安全地加密和存储密码

objective-c - UITableViewCell 不可选择,而它的accessoryView 可选择

ios - CollectionView中的程序化搜索栏实现问题