1
초보자 인 Swift와 ReactiveCocoa을 처음 사용했습니다. 나는 영화 목록을 보여주는 앱을 만들고 MVVM 패턴을 사용하고있다. 내 뷰 모델은 다음과 같습니다ReactiveCocoa (4.2.1) 및 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
}
}
}
}
와 나는이 같은 jQuery과 내 세포를 구성하고 싶습니다 :
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
}
이 반응성 코코아에 대한 올바른 접근 방식인가? Title과 Description을 Mutable 또는 그냥 이미지로 선언해야합니다 (단 하나만 변경됨). 나는 바인딩을 사용할 수 있다고 생각하지만 진행 방법을 모르겠습니다.