在 Rx 中自动将 UITableView 滚动到底部 Swift
Automatically Scroll UITableView to bottom in Rx Swift
我是 RX Swift 的新手,需要在 table 视图中显示数据,以便它自动显示 table 的最后一个单元格(默认滚动到底部)。
这是我绑定数据的代码:
private var dataArray = Variable<[Message]>([])
private let bag = DisposeBag()
dataArray.asObservable()
.bindTo(tableView.rx.items) { (tableView, row, msg) in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = obj.title
return cell
}
.addDisposableTo(bag)
此代码在 table 视图中显示数据,但不会在每次添加新单元格时将 table 视图滚动到底部。我可能需要添加以下行但不确定要使用哪个 rx-swift 运算符?
tableView.setContentOffset(CGPoint(x: 0, y : CGFloat.greatestFiniteMagnitude), animated: true)
提前致谢。
在相关说明中,如果有人能推荐一个用 rx-swift 制作的好的开源示例项目,我们将不胜感激。
每次 dataArray
发出新值时,您都需要设置内容偏移量。
一个好的解决方案是
dataArray.asObservable().map { CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude) }
.bindTo(tableView.rx.contentOffset)
.addDisposableTo(bag)
否则,你可以简单地做
dataArray.asObservable()
.do(onNext: {
self.tableView.setContentOffset(CGPoint(x: 0, y : CGFloat.greatestFiniteMagnitude), animated: true)
})
.bindTo(tableView.rx.items) { (tableView, row, msg) in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = obj.title
return cell
}
.addDisposableTo(bag)
do(onNext:)
用于在可观察对象发出值时执行副作用。这些副作用会发生每个订阅者一次。
更新@tomahh 的,
对于2022_01,对于NSObject_Rx
dataArray.asObservable().map{ _ in CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude) }
.bind(to: tableView.rx.contentOffset)
.disposed(by: rx.disposeBag)
我是 RX Swift 的新手,需要在 table 视图中显示数据,以便它自动显示 table 的最后一个单元格(默认滚动到底部)。 这是我绑定数据的代码:
private var dataArray = Variable<[Message]>([])
private let bag = DisposeBag()
dataArray.asObservable()
.bindTo(tableView.rx.items) { (tableView, row, msg) in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = obj.title
return cell
}
.addDisposableTo(bag)
此代码在 table 视图中显示数据,但不会在每次添加新单元格时将 table 视图滚动到底部。我可能需要添加以下行但不确定要使用哪个 rx-swift 运算符?
tableView.setContentOffset(CGPoint(x: 0, y : CGFloat.greatestFiniteMagnitude), animated: true)
提前致谢。
在相关说明中,如果有人能推荐一个用 rx-swift 制作的好的开源示例项目,我们将不胜感激。
每次 dataArray
发出新值时,您都需要设置内容偏移量。
一个好的解决方案是
dataArray.asObservable().map { CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude) }
.bindTo(tableView.rx.contentOffset)
.addDisposableTo(bag)
否则,你可以简单地做
dataArray.asObservable()
.do(onNext: {
self.tableView.setContentOffset(CGPoint(x: 0, y : CGFloat.greatestFiniteMagnitude), animated: true)
})
.bindTo(tableView.rx.items) { (tableView, row, msg) in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = obj.title
return cell
}
.addDisposableTo(bag)
do(onNext:)
用于在可观察对象发出值时执行副作用。这些副作用会发生每个订阅者一次。
更新@tomahh 的,
对于2022_01,对于NSObject_Rx
dataArray.asObservable().map{ _ in CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude) }
.bind(to: tableView.rx.contentOffset)
.disposed(by: rx.disposeBag)