带有 CollectionView/TableView 输入/文本字段的 RxSwift
RxSwift with CollectionView/TableView Input/ textfields
我正在尝试掌握 RxSwift,我需要验证表单,我在没有 tableView 的情况下完成了简单的验证,但现在我的文本输入字段在集合视图中,我想观察文本输入的变化,因为文本字段是现在在可重复使用的单元格中,我不确定如何添加可观察对象并从中获取流
基本上我想要将我的数据以 2 种方式绑定到输入是动态的表单,如果这有帮助的话
这是我的 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
}
我想使用 Rx 进行用户输入,而不是像下一行那样使用委托模式
txt.delegate = self
以下是我的屏幕图像
首先给你的自定义单元格它自己的处置袋用于 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)
}
我正在尝试掌握 RxSwift,我需要验证表单,我在没有 tableView 的情况下完成了简单的验证,但现在我的文本输入字段在集合视图中,我想观察文本输入的变化,因为文本字段是现在在可重复使用的单元格中,我不确定如何添加可观察对象并从中获取流
基本上我想要将我的数据以 2 种方式绑定到输入是动态的表单,如果这有帮助的话
这是我的 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
}
我想使用 Rx 进行用户输入,而不是像下一行那样使用委托模式
txt.delegate = self
以下是我的屏幕图像
首先给你的自定义单元格它自己的处置袋用于 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)
}