从URL下载UITableIViewCell图像

时间:2016-03-15 09:06:05

标签: ios swift uitableview alamofire sdwebimage

我正在使用alamofire从URL下载图像。 我正在获取图像onclick但是在视图出现时没有 如何在视图出现时获取所有图像?这是我用来实现的代码:

import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
import Kingfisher

class Paragogoi: UITableViewController {
var data=[FronItem]()   

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request(.GET, "http://gaiaskarpos.com/getCategores.php").validate().responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value 
            {
                let json = JSON(value)

                let list: Array<JSON> = json.arrayValue
                var urls=[String]()

                for element in list{
                    let id:String=element["id"].stringValue
                    let name:String=element["name"].stringValue
                    let url:String=element["url"].stringValue
                     self.data.append(FronItem(id:id,name:name,url:url))

                };
                self.tableView.reloadData() 

            }
        case .Failure(let error):
            print(error)
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

    return data.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("paragogosCell", forIndexPath: indexPath)

    cell.textLabel?.text = data[indexPath.row].name

    asycloadp(data[indexPath.row].url!, imageView: cell.imageView!)


    return cell
}

func asycloadp(url:String,imageView:UIImageView){
    let downloadQueue=dispatch_queue_create("myqueue", nil)

    print(url)
    dispatch_async(downloadQueue){
        let imageUrl:NSURL?
        = NSURL(string:url)
        dispatch_async(dispatch_get_main_queue()){
        imageView.sd_setImageWithURL(imageUrl)
        }

    }
}

}

我应该添加除此之外的任何内容吗?

2 个答案:

答案 0 :(得分:1)

从服务器获取所有数据后,只需在主线程中重新加载tableView

dispatch_async(dispatch_get_main_queue()) {
    self.tableView.reloadData()
}

答案 1 :(得分:1)

因为dispatch_async是异步传输,你必须在下载图像后刷新表视图,但我看到你正在使用SDWebImage,那么你不需要功能asycloadp,更改你的代码然后再试一次。

import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
import Kingfisher

class Paragogoi: UITableViewController {
    var data=[FronItem]()

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, "http://gaiaskarpos.com/getCategores.php").validate().responseJSON { response in
            switch response.result {
            case .Success:
                if let value = response.result.value {
                    let json = JSON(value)

                    let list: Array<JSON> = json.arrayValue
                    var urls=[String]()

                    for element in list{
                        let id:String=element["id"].stringValue
                        let name:String=element["name"].stringValue
                        let url:String=element["url"].stringValue
                        self.data.append(FronItem(id:id,name:name,url:url))

                    };
                    self.tableView.reloadData()

                }
            case .Failure(let error):
                print(error)
            }
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

        return data.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("paragogosCell", forIndexPath: indexPath)

        cell.textLabel?.text = data[indexPath.row].name

        let imageUrl:NSURL? = NSURL(string:url)
        cell.imageView!.sd_setImageWithURL(imageUrl,  completed:{(image: UIImage?, error: NSError?, cacheType: SDImageCacheType!, imageURL: NSURL?) in
            if (image != nil) {
                tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
            }
        })

        return cell
    }

}