2017-04-05 15 views
0

RxSwiftを把握しようとしています。フォームの検証が必要です。tableViewを使用しないで単純な検証を行っていますが、現在はテキスト入力フィールドがコレクションビューにあります。テキストフィールドは現在再利用可能なセルになっているので、私はオブザーバブルをどのように追加してストリームを取得するのか分かりませんCollectionView/TableViewを使用したRxSwift/textfields

基本的には、ここ

は私のcellForItemAt関数のコードである

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RegisterCell.identifier, for: indexPath) as! RegisterCell 
    var registerField = dataSource[indexPath.item] 
    registerField.indexPath = indexPath 
    registerField.text = viewModel.getText(indexPath: indexPath) 
    let txt = cell.txtfield as UITextField 
    txt.delegate = self 
    cell.configureCell(registerField) 
    return cell 
} 

func configureCell(_ fieldData: RegisterFields) { 
    txtfield.placeholder = fieldData.placeholderText 
    txtfield.isSecureTextEntry = fieldData.isSecureEntry 
    txtfield.text = fieldData.text 
    txtfield.tag = fieldData.indexPath.item 
    imgIcon.image = fieldData.image 
    imgDropDown.isHidden = !fieldData.isDropDown 
} 

私は次の行

txt.delegate =自己

次に行われるよう代わりにデリゲートパターンのユーザ入力のためのRxを使用したいが、のイメージであるセルに設定私の画面

enter image description here

+0

を私は、質問について知っているすべての詳細を追加しましたありがとうございます。 –

+0

テーブルデータをテーブルビューにバインドしていますか、通常のテーブルビューデリゲートを使用していますか?またはRxDataSource? –

+0

私は通常のテーブルビューデリゲートを使用しています。セルにテキストフィールドが1つあります。テキストフィールドにユーザ入力を聞きたいです。 –

答えて

1

まずテキストフィールドの入力ストリームに耳を傾け、あなたのコントローラに続いてのdeallocの目的のために、それは自身の処分バッグです

import RxSwift 

class YourCustomCell: UITableViewCell { 

    var disposeBag = DisposeBag() 

    @IBOutlet weak var textfield: UITextField! 

    override func prepareForReuse() { 
     super.prepareForReuse() 

     disposeBag = DisposeBag() 
    } 
} 

をカスタムセルを与える:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     guard let cell = cell as? YourCustomCell, textfield = cell.textfield else { return } 

     textfield 
      .rx.textInput.text.orEmpty 
      .asDriver() 
      .drive(onNext: { [unowned self] text in 
       //do what you want here 
      }) 
      .addDisposableTo(cell.disposeBag) 
    } 
関連する問題