ReactiveSwift - UIButton 部分

649 阅读1分钟

最近更新了 ReactiveCocoa 库的版本,发现了很多有趣的用法,简单记录一下

Action

点击事件+输入

Cell 绑定

func tableViewGuard(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell? {
let cell = tableView.dequeueReusableCell(withIdentifier: "GKTCollectionSectionMoreCell", for: indexPath) as! GKTCollectionSectionMoreCell
//cell.moreBtn.reactive.isEnabled <~ subViewModel.sectionMoreAction.isEnabled
cell.moreBtn.reactive.pressed = CocoaAction(subViewModel.sectionMoreAction, input: indexPath.section)
return cell
}

Action 实现

var sectionHeaderAction: Action <Int,Bool, Never> {
    return Action <Int, Bool, Never> {  (input) in
        return SignalProducer<Bool, Never> { [weak self] observer, _ in
                ... ... 
                observer.sendCompleted()
            }
        }
    }

监听

view.btn.reactive.controlEvents(.touchUpInside).observeValues { [weak self] btn in
    btn.isSelected = !btn.isSelected
}

如果在 Cell 中监听 signal 信号,需要处理

cell.moreBtn.reactive.controlEvents(.touchUpInside)
    .take(until: cell.reactive.prepareForReuse)
    .observeValues { [weak self] _ in
     }

参考资料 在使用ReactiveCocoa时,UITableViewCell里UIButton点击事件的一种错误实现