首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RxSwift,RxDataSources:如何使用RxDataSources将动态数据绑定到UITableView?

RxSwift,RxDataSources:如何使用RxDataSources将动态数据绑定到UITableView?
EN

Stack Overflow用户
提问于 2018-10-10 15:08:27
回答 1查看 4.1K关注 0票数 1

通过以下代码将一些数据绑定到UITableView之后:

代码语言:javascript
复制
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")


data = RxTableViewSectionedReloadDataSource<SectionOfCustomData>(configureCell: { dataSource, tableView, indexPath, item in

            if indexPath.section > 0 {

                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! TableViewCellTwo
                cell.age.text = "\(item.anInt)"
                return cell
            }else {

                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
                cell.name.text = item.aString
                cell.age.text = "\(item.anInt)"
                return cell
            }
        })

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)
    .bind(to: tableView.rx.items(dataSource: data!))
    .disposed(by: disposeBag)

在按下按钮并调用函数之后,我在var节中更改数据:

代码语言:javascript
复制
@IBAction func change(_ sender: UIButton) {

sections = [
            SectionOfCustomData(header: "third section", items: [CustomData(anInt: 4, aString: "four", aCGPoint: CGPoint.zero), CustomData(anInt: 5, aString: "five", aCGPoint: CGPoint(x: 1, y: 1)) ]),
            SectionOfCustomData(header: "fourth section", items: [CustomData(anInt: 6, aString: "six", aCGPoint: CGPoint(x: 2, y: 2)), CustomData(anInt: 7, aString: "seven", aCGPoint: CGPoint(x: 3, y: 3)) ])
        ]

但是在调用函数UITableView数据没有改变之后,我的问题是为什么在将节变量绑定到UITableView并在(部分)中更改数据之后,UITableView仍然显示最后的数据?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-10 15:44:53

因为这种变化是无法被观察到的。你需要的是-

代码语言:javascript
复制
// in class body
var dataSubject = PublishSubject<[SectionOfCustomData]>()

// in viewDidLoad() method
dataSubject
  .bind(to: tableView.rx.items(dataSource: data!))
  .disposed(by: disposeBag)

dataSubject.onNext([ /* your initial sections */ ])

// in change() method
dataSubject.onNext([ /* your new sections */ ])
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52743367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档