gpt4 book ai didi

ios - 在 RxSwift 的 UICollectionViewCell 中订阅 UIButton 点击​​?

转载 作者:行者123 更新时间:2023-11-28 06:30:39 25 4
gpt4 key购买 nike

我是 RxSwift 的新手,正在努力了解它。我无法让单元格中的 UIButton 在按下时显示 UIAlertController。

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)
}

按下时没有任何反应。我在这里做错了什么?

最佳答案

我实际上更喜欢在其子类上分配单元格按钮操作。问题是我认为每个单元格都应该有它自己的 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()
}

关于ios - 在 RxSwift 的 UICollectionViewCell 中订阅 UIButton 点击​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40458241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com