2016-11-07 39 views
1

저는 RxSwift를 처음 사용하여 저의 머리를 감싸려고합니다. 내가 눌렀을 때 UIAlertController를 보여주기 위해 셀에 UIButton을 가져 오는 데 문제가있었습니다.RxSwift에서 UICollectionViewCell의 UIButton 탭에 구독 하시겠습니까?

private func setupCellConfiguration() { 
     bookListViewModel.data 
      .bindTo(collectionView.rx.items(cellIdentifier: BookListCell.Identifier, cellType: BookListCell.self)) { [unowned self] (row, element, cell) in 
       cell.configureForBook(book: element) 
       cell.moreButton.rx.tap.subscribe { [weak self] in 
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {(action) in 
         self?.dismiss(animated: true, completion: nil) 
        } 
        alertController.addAction(cancelAction) 
        let destroyAction = UIAlertAction(title: "Delete", style: .destructive) { (action) in 

        } 
        alertController.addAction(destroyAction) 
        self?.present(alertController, animated: true) 
       } 
       .addDisposableTo(self.disposeBag) 
      } 
      .addDisposableTo(disposeBag) 
} 

아무 것도 발생하지 않습니다. 여기서 내가 뭘 잘못하고 있니?

+0

당신은 자기가 전무 아니었다면 한 번 확인 Xcode의 디버거를 사용해 본 적이 알려하고, 코드를 테스트하지 않았나요? 그렇게하면 문제가 어디에 있는지 (즉, Rx 문제인지 또는 참조 문제인지) 파악할 수 있습니다. – iwillnot

답변

1

실제로 하위 클래스에 셀 단추 동작을 할당하는 것을 선호합니다. 문제는 모든 셀이 자체 disposeBag를 가져야하며 재사용 할 때마다 다시 초기화해야한다고 생각합니다.

예 :이 있다면 경고 컨트롤러가 성공적으로 인스턴스화 된 경우 어떤 문제가 나에게

private func setupCellConfiguration() { 
bookListViewModel.data 
    .bindTo(collectionView.rx.items(cellIdentifier: BookListCell.Identifier, cellType: BookListCell.self)) { [unowned self] (row, element, cell) in 

     cell.delegate = self 
     cell.configureForBook(book: element) 
    } 
    .addDisposableTo(disposeBag) 
} 

// Your Cell Class 
var disposeBag = DisposeBag() 
var delegate: UIViewController? 

func configureForBook(book: Book) { 

    self.moreButton.rx.tap.subscribe { [unowned self] in 

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {(action) in 
     self?.dismiss(animated: true, completion: nil) 
    } 
    alertController.addAction(cancelAction) 
    let destroyAction = UIAlertAction(title: "Delete", style: .destructive) { (action) in 

    } 
    alertController.addAction(destroyAction) 
    self.delegate?.present(alertController, animated: true) 
    } 
    .addDisposableTo(self.disposeBag) 
} 

override func prepareForReuse() { 
    disposeBag = DisposeBag() 
}