我正在尝试使用RxSwift创建一个包含多个部分的表视图.每个部分显示代表不同类型的数据.
我找到了RxSwiftDataSources
库并从他们的文档中实现了示例.
以下是该示例如何实现的快速问题:
定义了自定义数据类型CustomData
:
struct CustomData {
var anInt: Int
var aString: String
var aCGPoint: CGPoint
}
Run Code Online (Sandbox Code Playgroud)
然后,添加该部分的表示(注意SectionModelType
这里实现):
struct SectionOfCustomData {
var header: String
var items: [Item]
}
extension SectionOfCustomData: SectionModelType {
typealias Item = CustomData
init(original: SectionOfCustomData, items: [Item]) {
self = original
self.items = items
}
}
Run Code Online (Sandbox Code Playgroud)
最后,创建一些示例数据并将其绑定到表视图:
let sections: [SectionOfCustomData] = [
SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) …
Run Code Online (Sandbox Code Playgroud) 我想知道 reloadTableView 的结束然后我想向下滚动到表格视图的底部。
在我使用之前RxSwift
就在之后reloadData
可以使用setContentOffSet
或ScrollToRow
。
我用我找到的代码试了一下。从来没有叫过endUpdates
。
var replyList : BehaviorRelay<[Reply]>!
func bind(){
replyViewModel.replyList
.asDriver()
.drive(replyTableView.rx.items){ [weak self] (tableView,row,item) in
return self?.makeReplyCell(tableView: tableView, replyInfo: item) ?? UITableViewCell()
}
.disposed(by: disposeBag)
replyTableView.endUpdatesEvent
.asObservable()
.subscribe({ _ in
print("Scroll To Bottom")
})
.disposed(by: disposeBag)
}
import Foundation
import RxCocoa
import RxSwift
extension UITableView {
/// Reactive wrapper for `UITableView.insertRows(at:with:)`
var insertRowsEvent: ControlEvent<[IndexPath]> {
let source = rx.methodInvoked(#selector(UITableView.insertRows(at:with:)))
.map { a in
return a[0] as! [IndexPath] …
Run Code Online (Sandbox Code Playgroud) 嘿我正在尝试获取一个UICollectionView,由一个使用RxCocoa和RxDataSources的UICollectionViewController托管.
当我使用带有嵌入式UICollectionView的UIViewController时,一切正常.
但是当我尝试通过相同的逻辑连接时:
self.vm.sections
.bind(to: self.collectionView!.rx.items(dataSource: self.vm.data))
.disposed(by: self.bag)
Run Code Online (Sandbox Code Playgroud)
在UICollectionViewController内部使用UICollectionView,Xcode完全崩溃.
有没有关于RxDataSources的东西,你不能将它们与UICollectionViewController一起使用?
这是 RxSwift 中的 tableView 我无法配置数据源。RxTableViewSectionedReloadDataSource 似乎缺少参数,尽管这很奇怪,因为我遵循官方文档的完全相同的代码源
Xcode 错误 每当我按回车键自动完成关闭。关闭保持空白。
自动完成无效 我真的不知道如何解决这个问题
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel>()
dataSource?.configureCell = { (ds: RxTableViewSectionedReloadDataSource<SectionOfCustomData>, tv: UITableView, ip: IndexPath, item: Article) -> NewsFeedCell in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: ip) as! NewsFeedCell
cell.configure(news: item)
return cell
}
dataSource?.titleForHeaderInSection = { ds, index in
return ds.sectionModels[index].header
}
let sections = [
SectionOfCustomData(header: "First section", items: self.articles),
SectionOfCustomData(header: "Second section", items: self.articles)
]
guard let dtSource = dataSource else {
return
}
Observable.just(sections)
.bind(to: …
Run Code Online (Sandbox Code Playgroud) 假设我有UIButton
一个UITableViewCell
.从UITableView
我想要订阅的单元格出列后UIButton.rx.tap
.问题是如果我UITableViewCell
多次出列,订阅将保留.目前我通过Disposable
在my中分配属性,在UITableViewCell
创建订阅时设置它并调用Disposable.dispose()
来解决这个问题UITableViewCell.prepareForReuse()
,但据我所知,实现功能的方式要求你调用Disposable.dispose()
暗示你做错了.
有没有更好的方法来实现订阅的唯一性而无需重新分配UIButton
?
struct MyViewModel {
var items: Observable<String>
//....
}
// In view controller
viewModel.items.bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: MyCell.self)) { index, model, cell in
//...
}
.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
如果我有另一个被调用的单元格EmptyCell
,并且我想在项目为空时显示此单元格.我怎么能实现这一点.
我有一个带有 RxDataSources 的表格视图,其中单元格项目有一个删除图标。当单元格出队并单击该删除图标时,所有先前的单击事件都会被触发,从而重复点击。项目单元格:
removeImageView.rx.tap().map { _ in indexPath }
.bind(to: viewModel.onRemoveItem).disposed(by: cellDisposeBag)
Run Code Online (Sandbox Code Playgroud)
单元格视图模型:
let onRemoveItem = PublishSubject<IndexPath>()
Run Code Online (Sandbox Code Playgroud)
单元格和 ViewModel 绑定的视图控制器视图模型:
let vm = ItemViewModel(with: item)
vm.onRemoveItem.bind(to: self.onRemoveItem).disposed(by: self.rx.disposeBag)
return SectionItem.item(viewModel: vm)
Run Code Online (Sandbox Code Playgroud)
视图控制器:
let dataSource = RxTableViewSectionedReloadDataSource<SectionItem>(configureCell: { dataSource, tableView, indexPath, item in
switch item {
case .item(let viewModel):
let cell = (tableView.dequeueReusableCell(withIdentifier: itemtIdentifier, for: indexPath) as? ItemCell)!
cell.bind(to: viewModel, at: indexPath)
return cell
}
}, titleForHeaderInSection: { dataSource, index in
let section = dataSource[index]
return section.title
} )
output?.items
.bind(to: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试绑定(到 :) 一个 collectionView,但 tableView 也不起作用。我有一个 viewModel,我的 Variable<[]> 在哪里,我想在值更改时使用我的 tableView 进行订阅。
viewModel.theVariable
.asObservable()
.bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)){
(row, item, cell) in
cell.textLabel?.text = item
}
.addDisposableTo(disposeBag)
Run Code Online (Sandbox Code Playgroud)
XCode 告诉我Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible'
应该是哪个,因为它适用于任何 UIView。
我已经尝试过使用它的 just() Observable 并且这种方法似乎可以正常工作。问题是我需要有一个变量,我在 viewModel 中设置了一个值,在 View 中我需要观察这个变化。不确定 Observable 是否提供此方法。
关键是这应该适用于变量吗?这是一个错误吗?我使用 Swift 3.2
我使用 RxSwift 在我的 tableview 中显示人员列表,我的 tableview 有两个部分,第一个是旧搜索,第二个是所有人员。现在我不知道当用户在 UISearchBar 的文本字段上输入名称时我应该如何过滤人员。
这是我的人模型:
struct PersonModel {
let name: String
let family:String
let isHistory:Bool
}
Run Code Online (Sandbox Code Playgroud)
这是我的 ContactsViewModel
struct SectionOfPersons {
var header: String
var items: [Item]
}
extension SectionOfPersons: SectionModelType {
typealias Item = PersonModel
init(original: SectionOfPersons, items: [SectionOfPersons.Item]) {
self = original
self.items = items
}
}
class ContactsViewModel {
let items = PublishSubject<[SectionOfPersons]>()
func fetchData(){
var subItems : [SectionOfPersons] = []
subItems.append( SectionOfPersons(header: "History", items: [
SectionOfPersons.Item(name:"Michelle", family:"Obama", isHistory:true ),
SectionOfPersons.Item(name:"Joanna", family:"Gaines", …
Run Code Online (Sandbox Code Playgroud) 目前,对于我们的 API 请求,我们使用 Rx。我们如何使用它的一个例子是:
let orderRxService = OrderRxService.listAsShop(shopId, status: .active)
.repeatRequest(delay: 4)
.observeOn(MainScheduler.instance)
.subscribe( onNext: { [weak self] orders in
self?.orders = orders
self?.tableView.reloadData()
})
.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
这将获取所有shopId
状态为给定的订单.active
。每次更新时,都会替换本地orders
对象并重新加载 tableView。
这会重新加载整个 tableView,这是我们想要避免的。我现在正在研究 RxDataSources,但无法真正弄清楚实现此功能的方法是什么。
一个Order
对象有另一个属性currentStatus
,它可以是 3 个不同的值。我们拥有的是一个包含 3 个不同部分的 tableView,每个部分显示currentStatus
.
这应该如何在 RxDataSources 中实现?理想的情况是将其绑定到我之前展示的服务 ( OrderRxService.....subscribe()..
)。
我现在要设置的 RxDataSources 类型是:
extension Order: IdentifiableType, Equatable {
public typealias Identity = String
public var identity: String {
return String(id)
}
public static func …
Run Code Online (Sandbox Code Playgroud) 我正在构建一个由 RxDataSources 支持的表视图。我想启用此表视图的编辑,以便用户可以删除项目。
我目前有这个代码:
var strings = [String]() {
didSet {
observable.accept(strings)
}
}
let observable = BehaviorRelay(value: [String]())
let disposeBag = DisposeBag()
override func viewDidLoad() {
tableView.dataSource = nil
let dataSource = RxTableViewSectionedAnimatedDataSource<StringSection>(configureCell: {
(dataSource, collectionView, indexPath, string) -> UITableViewCell in
let cell = collectionView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = string
return cell
})
observable.asObservable()
.map {
[StringSection(items: $0)]
}
.bind(to: self.tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
// X
strings = ["Item 1", "Item 2", "Item 3"]
}
Run Code Online (Sandbox Code Playgroud)
为了使其可编辑,我将其添加到标记为 X 的位置: …
我有一个问题:如何使用RxDataSources以Rx方式正确实现这样的场景:
我们有一个带有UICollectionView(或UITableView,在我的情况下是它的集合视图)的类,结果不会立即出现,它们会在一段时间后异步出现.
我已根据本教程中的部分实现了我的模型:https: //github.com/RxSwiftCommunity/RxDataSources
但是数据只在just
那里创建一次:
let sections = [
SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) ]),
SectionOfCustomData(header: "Second section", items: [CustomData(anInt: 2, aString: "two", aCGPoint: CGPoint(x: 2, y: 2)), CustomData(anInt: 3, aString: "three", aCGPoint: CGPoint(x: 3, y: 3)) ])
]
Observable.just(sections)
.bindTo(collectionView.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag)
Run Code Online (Sandbox Code Playgroud)
如果我的项目在一段时间后可用,我希望我的集合视图能够自动更新,该怎么办?
谢谢你的帮助.
通过以下代码将一些数据绑定到 UITableView 后:
struct CustomData {
var anInt: Int
var aString: String
var aCGPoint: CGPoint
}
struct SectionOfCustomData {
var header: String
var items: [CustomData]
}
extension SectionOfCustomData: SectionModelType {
init(original: SectionOfCustomData, items: [CustomData]) {
self = original
self.items = items
}
}
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let disposeBag = DisposeBag()
var data: RxTableViewSectionedReloadDataSource<SectionOfCustomData>?
override func viewDidLoad() {
super.viewDidLoad()
let x = status.asObservable()
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
tableView.register(UINib(nibName: "TableViewCellTwo", bundle: nil), forCellReuseIdentifier: "Cell2") …
Run Code Online (Sandbox Code Playgroud) rxdatasources ×13
rx-swift ×11
swift ×10
ios ×9
uitableview ×4
reactivex ×1
uibutton ×1
uisearchbar ×1