2016-04-17 23 views
0

這裏是我Movie類:的NSOperation加載圖像的TableView

import UIKit 

class Movie { 
    var title: String = "" 
    var rating: Double = 0 
    var image: UIImage = UIImage() 
} 

我想要的movie的陣列負載tableView,我已經試過這樣:

import UIKit 
import Cosmos 
import Alamofire 
//import AlamofireImage 
import SwiftyJSON 

class Downloader { 
    class func downloadImageWithURL(url:String) -> UIImage! { 
     let data = NSData(contentsOfURL: NSURL(string: url)!) 
     return UIImage(data: data!) 
    } 
} 

class MovieViewController: UIViewController, UITableViewDataSource { 

    @IBOutlet var tableView: UITableView! 
    var movies: [Movie] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     fetchMovies() 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MovieTableViewCell 
     let movie = movies[indexPath.row] 
     cell.movieTitleLabel.text = movie.title 
     cell.movieRatingView.rating = Double(movie.rating/20) 
     cell.movieImageView.image = movie.image 
     return cell 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return movies.count 
    } 

    func fetchMovies() { 
     let movieURLString = "https://coderschool-movies.herokuapp.com/movies?api_key=xja087zcvxljadsflh214" 

     Alamofire.request(.GET, movieURLString).responseJSON { response in 

      let json = JSON(response.result.value!) 
      let movies = json["movies"].arrayValue 
      let queue = NSOperationQueue() 
      for movie in movies { 
       let title = movie["title"].string 
       let rating = movie["ratings"]["audience_score"].double 
       let imageURLString = movie["posters"]["thumbnail"].string 

       let movie = Movie() 
       movie.title = title! 
       movie.rating = rating! 

       let operation = NSBlockOperation(block: { 
        movie.image = Downloader.downloadImageWithURL(imageURLString!) 
       }) 
       queue.addOperation(operation) 

       self.movies.append(movie) 
       self.tableView.reloadData() 

      } 
     } 
    } 
} 

的問題是即:當我向下或向上滾動時,圖像將被重新加載,否則它們不會被重新加載。只有titlerating

enter image description here

我只知道原因是這些線

self.movies.append(movie) 
self.tableView.reloadData() 

let operation = NSBlockOperation(block: { 
    movie.image = Downloader.downloadImageWithURL(imageURLString!) 
}) 
queue.addOperation(operation) 

編譯但如果我向下滾動,它會喜歡這個:

enter image description here

我已經用AlamofireImage完成了,但我真的想用NSOperation來潛水。

回答

0

我通過把reloadData解決問題爲主線,像這樣:

let operation = NSBlockOperation(block: { 
        movie.image = Downloader.downloadImageWithURL(imageURLString!) 
        NSOperationQueue.mainQueue().addOperationWithBlock() { 
         self.tableView.reloadData() 
        } 
       }) 

現在它工作得很好。

1

我認爲你應該確保reloadData()在主線程上。默認情況下,它發生在後臺線程上,無法更新UI。

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

是的,正確:) – Khuong

+0

請檢查我的答案與NSOperation。 – Khuong