2016-06-07 71 views
0

我試圖從這個data source解析數據,標題被正確解析並顯示,但圖像根本不顯示,也沒有緩存。 下面是我對MyTableViewController.swift代碼和休息類是這樣一個問題:Parsing image from iTunes using JSON to tableviewcontrollerSwift:圖像緩存不能正常工作

class MyTableViewController: UITableViewController { 
@IBOutlet var songsTable: UITableView! 
let viewModel = ViewModel() 
var imageCache = [String:UIImage]() 
var songs = [NSManagedObject]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.refresh() 
    self.refreshControl = UIRefreshControl() 
    self.refreshControl?.addTarget(self, action: #selector(MyTableViewController.refresh), forControlEvents: .ValueChanged) 

} 

func refresh() { 
    viewModel.fetch { 
     dispatch_async(dispatch_get_main_queue()) { 
      self.tableView.reloadData() 
      self.refreshControl?.endRefreshing() 
     } 
    } 


} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return self.viewModel.numberOfSections() 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.viewModel.numberOfItemsInSection(section) 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell 
    cell.songTitle.text = self.viewModel.titleForItemAtIndexPath(indexPath) 
    //let song = songs[indexPath.row] 
    //cell.songTitle.text = song.valueForKey("title") as? String 


    let urlString = self.viewModel.imageForItemAtIndexPath(indexPath) 

    let imgURL: NSURL = NSURL(string: urlString)! 
    let request: NSURLRequest = NSURLRequest(URL: imgURL) 
    NSURLConnection.sendAsynchronousRequest(
     request, queue: NSOperationQueue.mainQueue(), 
     completionHandler: {(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in 
      if error == nil { 
       cell.songImage.image = UIImage(data: data!) 
      } 
    }) 

    let thumbnailURLString = self.viewModel.imageForItemAtIndexPath(indexPath) 
    let thumbnailURL = NSURL(string: thumbnailURLString)! 

    //if image is cached 
    if let img = imageCache[thumbnailURLString] { 
     cell.songImage?.image = img 
     print("image is cached") 
    } 
    else { 
     // The image isn't cached, download the img data 
     // We should perform this in a background thread 
     let request: NSURLRequest = NSURLRequest(URL: thumbnailURL) 
     let mainQueue = NSOperationQueue.mainQueue() 
     NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 
      if error == nil { 
       // Convert the downloaded data in to a UIImage object 
       let image = UIImage(data: data!) 
       // Store the image in to our cache 
       self.imageCache[thumbnailURLString] = image 
       // Update the cell 
       dispatch_async(dispatch_get_main_queue(), { 
        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as! MyTableViewCell?{ 
         cellToUpdate.songImage?.image = image 
        } 
       }) 
      } 
      else { 
       print("Error: \(error!.localizedDescription)") 
      } 
     }) 
    } 

    return cell 
} 

感謝您抽出時間來閱讀。

回答

1

你的代碼很混亂,但似乎工作。我能得到它的工作提供了我改變你的refresh功能是這樣的:這是利用你以前的問題發佈的代碼

func refresh() { 
     viewModel.fetchTitles({[unowned self] _ in 
      self.viewModel.fetchImages({[unowned self] _ in 
       dispatch_async(dispatch_get_main_queue()) { 
        self.tableView.reloadData() 
        self.refreshControl?.endRefreshing() 
       } 
      }) 
     }) 
    } 

。你可以試試這個或郵政編碼爲viewModel.fetch

+0

非常感謝:D – Reem