2016-03-15 120 views
0

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

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) 
     } 

    } 
} 

} 

我要补充这比其他东西吗?

回答

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 
    } 

} 
+0

是的,我尝试这种情况,而是当点击细胞获取图像。 –

+0

@adhslock我再次更新了代码,这个应该可以工作,我已经测试过了。 – BobGao

+0

是的,它工作。感谢您的帮助。 –

1

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

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

我看不到图像execpt让单击单元格 –