简体   繁体   English

如何使用 RxSwift 将数据添加到 tableview 或 collectionview

[英]How to prepend data to tableview or collectionview using RxSwift

I can see that I can use Subject like Publish or Variable to add new element to table view or collection view and that will show properly at the end but what if need to add new data at the beginning of the table view.我可以看到我可以使用像PublishVariable这样的Subject向表视图或集合视图添加新元素,这将在最后正确显示,但是如果需要在表视图的开头添加新数据怎么办。 what to do then??那怎么办??

How can I prepend data to a observable sequence so that new data shows at the top of tableview or collection view ???如何将数据添加到可观察序列,以便新数据显示在表视图或集合视图的顶部???

You can use combineLatest operator.您可以使用combineLatest运算符。

Initially, newData is an empty array, and when you add data to it, it is displayed at the top of tableView一开始newData是一个空数组,当你newData添加数据时,它会显示在tableView的顶部

    let data = [
        "A New Hope",
        "The Empire Strikes Back",
        "Return of the Jedi"
    ]

    let obsData = Observable.just(data)

    let newData = [
        "The Phantom Menace",
        "Attack of the Clones",
        "Revenge of the Sith"
    ]
    let obsNewData = Observable.just(newData)

    let items = Observable.combineLatest(obsData, obsNewData){
        $1+$0 
    }     

    items.bindTo(tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)) { (row, film, cell) in
        cell.textLabel?.text = film
    }
    .addDisposableTo(disposeBag)

A complete look of what i was looking for我正在寻找的完整外观

import UIKit
import RxSwift
import RxCocoa

struct Person {
    let name : String
    let age : Int

}
class ViewController: UIViewController {
    let personArray = [
        Person(name: "name1", age: 11),
        Person(name: "name2", age: 12),
        Person(name: "name3", age: 13)
    ]

    let disposeBag = DisposeBag()
    var items : Observable<[Person]>!

    @IBOutlet weak var mytable: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        items = Observable.just(personArray)
        bindData()
    }

    @IBAction func prependButtonAction(_ sender: UIButton) {
        let newPersonArray = [
            Person(name: "name -3", age: 11),
            Person(name: "name -4", age: 12)
        ]
        prependData(dataToPrepend: newPersonArray)
    }

    @IBAction func appendButtonAction(_ sender: UIButton) {
        let newPersonArray = [
            Person(name: "name 6", age: 11),
            Person(name: "name 7", age: 12)
        ]
        appendData(dataToAppend : newPersonArray)
    }



    private func bindData() {
        mytable.dataSource = nil
        items.bindTo(mytable.rx.items(cellIdentifier: "cell")) { (row, person, cell) in
            if let cellToUse = cell as? TableViewCell {
                cellToUse.label1.text = person.name
                cellToUse.label2.text = "\(person.age)"
            }
        }.addDisposableTo(disposeBag)
    }

    private func prependData(dataToPrepend : [Person]) {
        let newObserver = Observable.just(dataToPrepend)
        items = Observable.combineLatest(items, newObserver) {
            $1+$0
        }
        bindData()
    }


    private func appendData(dataToAppend : [Person]) {
        let newObserver = Observable.just(dataToAppend)
        items = Observable.combineLatest(items, newObserver) {
            $0+$1
        }
        bindData()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM