Swift 4 AlamofireImage with UITableView Cell

后端 未结 10 3111
梦如初夏
梦如初夏 2021-02-20 12:56

I am using AlamofireImages to try to download an image from a server and display it in my table view cell, this is what I got, but my images does not appear, just the textLabel

10条回答
  •  Happy的楠姐
    2021-02-20 13:55

    You should pass the link as a variable to a variable in the cell and override the awakefromnib function in UITableViewCell. Inside the awakeFrom nib you call the alamofire request to load the image in the cell, with this way you will not call the query every time it reuses the cell.

    so

    //CellCode

    import Alamofire
    ...
    
        var imageLink:String?
    
        override func awakeFromNib() {
                super.awakeFromNib()
        Alamofire.request(imageLink).responseImage { response in
                    if let image = response.result.value {
                        self.imageView?.image = image
                    }
                }
        }
    
    
    //cell configuration
    cell.imageLink = "http://xxxxxxx.com/uploads/" + self.array[indexPath.row]["cover"] as! String)
    

提交回复
热议问题