在tableView(_:cellForRowAt :)中下载图像,导致单元格不停闪烁

时间:2017-05-18 12:15:46

标签: ios swift uitableview

我想在方法UITableViewCell上为每个tableView(_:cellForRowAt:)懒得下载图片。当表格视图显示在屏幕上时,它会在每个单元格上不停地闪烁,我无法滚动tableView。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MessageCell", for: indexPath) as! MessageCell
    let row = indexPath.row

    cell.content.delegate = self
    cell.content.text = messages[row].content
    cell.date.text = messages[row].createdDateStrInLocal
    cell.messageOwner.text = messages[row].user

    self.fetchUserAvatar(avatarName: messages[row].user, handler: ({ img in
        cell.profileImageView.image = img
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }))
    return cell
}

fileprivate func fetchUserAvatar(avatarName: String, handler: @escaping (UIImage) -> Void){
    guard !avatarName.isEmpty, let user = self.user, !user.isEmpty else { return }
        let url = URL(string: self.url + "/userAvatarURL")
        var request = URLRequest(url: url!)
        let body = "username=" + user + "&avatarName=" + avatarName
        request.httpMethod = "POST"
        request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.httpBody =  body.data(using: .utf8)
        defaultSession.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
            guard error == nil else {
                print(error!)
                return
            }
            if let httpResponse = response as? HTTPURLResponse, 200...299 ~= httpResponse.statusCode, let data = data, let urlStr = String(data: data, encoding: String.Encoding.utf8), urlStr != "NULL", urlStr.contains("http"), let url = URL(string: urlStr) {
                self.defaultSession.dataTask(with: url, completionHandler:{ data, respone, error in
                    guard error == nil else {
                        print(error!)
                        return
                    }
                    DispatchQueue.main.async {
                        if let httpResponse = response as? HTTPURLResponse, 200...299 ~= httpResponse.statusCode, let data = data, let img = UIImage(data: data) {
                            self.cacheImage.setObject(img, forKey: avatarName as NSString)
                            handler(img)
                        }
                    }
                }).resume()
            }
        }).resume()
}

2 个答案:

答案 0 :(得分:3)

尽管这不是将图像下载到单元格的安全方法,但您需要删除tableView.reloadRows(at: [indexPath], with: .automatic)。这会导致无限循环。

确保在更新图像时更新约束。

答案 1 :(得分:0)

对于fetchUserAvatar,请将其设置在单独的后台线程中。在完成块中,返回主线程并更新表格单元格拇指图像。永远不要在`cellForRowAt indexPath:IndexPath'中重新加载表视图。因为它已经在重新加载细胞。