TableView自定义滑动操作-Rxswift



以下是我如何在代码中使用RxSwift:

ViewModel中的我的新闻变量:

var news = PublishSubject<[Article]>()

将Rxswift与tableview一起使用的扩展。

extension HomeViewController {
func designTableView() {
newsTable.separatorStyle = .none
newsTable.showsVerticalScrollIndicator = false
}
func createCellView() {
homeViewModel.news.bind(to: newsTable.rx.items(cellIdentifier: "cell", cellType: NewsCell.self)) { _, news, cell in
cell.newsTitle.text = news.title
cell.newsImage?.sd_setImage(with: URL(string: news.urlToImage ?? ""), placeholderImage: UIImage(systemName: "photo"))
}.disposed(by: disposeBag)
}
func whenNewsSelected() {
newsTable.rx.modelSelected(Article.self).bind { article in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "NewsDetail") as? NewsDetails
vc?.article = article
self.navigationController?.pushViewController(vc!, animated: true)
}.disposed(by: disposeBag)
}
func setDelegateForTableView() {
newsTable.rx.setDelegate(self).disposed(by: disposeBag)
}

}

我想做的是,将"收藏夹"操作添加到单元格中。我尝试了下面的代码,但我不知道如何访问那一行的新闻数据。

func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let FlagAction = UIContextualAction(style: .normal, title: "Fav", handler: { (_: UIContextualAction, _: UIView, success: (Bool) -> Void) in
print("osman")
success(true)
})
FlagAction.backgroundColor = .orange
return UISwipeActionsConfiguration(actions: [FlagAction])
}

有人能帮我吗?

使用let news = BehaviorSubject<[Article]>(value: [])代替var news = PublishSubject<[Article]>()(注意:受试者应始终为常量,而不是变量(

然后当你需要访问文章时,你可以做:

let articles = (try? homeViewModel.news.value()) ?? []

我通常不建议使用Subject,但这里的替代方案是编写自己的DataSource类型或使用RxDataSources,这对你来说可能太难了。

如果你真的想进入兔子洞

引入RxDataSources或编写自己的数据源,并使其成为视图控制器的成员:

let dataSource = MyDataSource<Article, NewsCell>(cellIdentifier: "cell") { _, news, cell in
cell.newsTitle.text = news.title
cell.newsImage?.sd_setImage(with: URL(string: news.urlToImage ?? ""), placeholderImage: UIImage(systemName: "photo"))
}

这样使用:

func createCellView() {
homeViewModel.news
.bind(to: newsTable.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}

然后你可以访问这样的文章:

let articles = dataSource.elements

以下是基本数据源的样子。这将完成默认RxCocoa数据源所做的一切。

class MyDataSource<Element, Cell>: NSObject, UITableViewDataSource, RxTableViewDataSourceType
where Cell: UITableViewCell {
private(set) var elements: [Element] = []
private let cellIdentifier: String
private let configureCell: (Int, Element, Cell) -> Void
init(cellIdentifier: String, configureCell: @escaping (Int, Element, Cell) -> Void) {
self.cellIdentifier = cellIdentifier
self.configureCell = configureCell
}
func tableView(_ tableView: UITableView, observedEvent: RxSwift.Event<[Element]>) {
guard case let .next(elements) = observedEvent else { return }
self.elements = elements
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
elements.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? Cell else {
return UITableViewCell()
}
let element = elements[indexPath.row]
configureCell(indexPath.row, element, cell)
return cell
}
}

最新更新