Ich bin ein Anfänger mit ReactiveCocoa mit Swift zum ersten Mal. Ich erstelle eine App mit einer Liste von Filmen und verwende das MVVM-Muster. Mein Ansichtsmodell sieht wie folgt aus:Async Bild laden mit ReactiveCocoa (4.2.1) und Swift
class HomeViewModel {
let title:MutableProperty<String> = MutableProperty("")
let description:MutableProperty<String> = MutableProperty("")
var image:MutableProperty<UIImage?> = MutableProperty(nil)
private var movie:Movie
init (withMovie movie:Movie) {
self.movie = movie
title.value = movie.headline
description.value = movie.description
Alamofire.request(.GET, movie.pictureURL)
.responseImage { response in
if let image = response.result.value {
print("image downloaded: \(image)")
self.image.value = image
}
}
}
}
und ich möchte meine Zellen im UITableView so konfigurieren:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MovieCell", forIndexPath: indexPath) as! MovieCell
let movie:Movie = movieList[indexPath.row]
let vm = HomeViewModel(withMovie: movie)
// fill cell with data
vm.title.producer.startWithNext { (newValue) in
cell.titleLabel.text = newValue
}
vm.description.producer.startWithNext { (newValue) in
cell.descriptioLabel.text = newValue
}
vm.image.producer.startWithNext { (newValue) in
if let newValue = newValue {
cell.imageView?.image = newValue as UIImage
}
}
return cell
}
Ist das der richtige Ansatz für Reactive Cocoa? Muss ich den Titel und die Beschreibung als Veränderbar oder nur als Bild deklarieren (als einziger veränderlich). Ich denke, ich könnte binden, aber ich bin mir nicht sicher, wie es weitergehen soll.