使用Almofireimage在自定义Tableview单元格中设置图像

时间:2017-01-25 02:33:23

标签: ios swift uitableview alamofireimage

我正在使用Alamofireimage在我的自定义UIImageView中基于UITableViewCell上的远程网址设置图像,但结果是(1)在您滚动之前图像未设置并且( 2)即使我使用StackViews进行自动布局,应用程序中显示的图像大小也有很大差异。知道我做错了什么吗?

import UIKit
import Alamofire
import AlamofireImage

class AppsTableViewController: UITableViewController {

    let session = URLSession.shared

    var rekos: [Reko]? {
        didSet {
            DispatchQueue.main.async {
                self.tableView?.reloadData()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        RekoManager().downloadAndConvertRekos(rekoType: .apps) { (result) in
            print("Reko Count = \(result.count)")
            self.rekos = result
        }


    }



    // MARK: - Table view data source


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

          guard let count = rekos?.count else {return 0}
        return count 
    }

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

        if let reko = rekos?[indexPath.row] {



            cell.titleLabel.text = reko.title
            cell.descriptionLabel.text = reko.description
            if let imageUrlString = reko.imageUrlString {

                if let imageURL = URL(string: imageUrlString) {

                    //TODO IMPLEMENT SPINNER OVER IMAGE
                    cell.appImageView.af_setImage(withURL: imageURL, placeholderImage: UIImage(named: "placeholder"), filter: nil, progress: nil, progressQueue: DispatchQueue.main, imageTransition: .noTransition, runImageTransitionIfCached: false, completion: { (result) in
                        cell.setNeedsLayout()
                        cell.layoutIfNeeded()
                    })

                }
            }
        }
        return cell
    }


}

0 个答案:

没有答案