RxSwift官方实例十(刷新)

代码下载

搭建UI

新建控制器,搭建两个UITableView和一个UICollectionView作为控制器属性:

    @IBOutlet weak var partialTableView: UITableView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var partialCollectionView: UICollectionView!

在控制器的viewDidLoad函数中为导航栏的右侧设置一个item

        // 构建UI
        let rightItem = UIBarButtonItem(title: "更新", style: .plain, target: nil, action: nil)
        self.navigationItem.rightBarButtonItem = rightItem

数据生成

定义一个数据类型作为本示例的数据类型NumberSection

typealias NumberSection = SectionModel<Int, Int>

定义一个数据混乱器Randomizer,作用就是将[NumberSection]这样的数据打散、打乱。sections属性存储结果数据,randomize函数混乱数据。具体实现查看代码……

定义一个UpdatesViewModel

struct UpdatesViewModel {
    private var generator = Randomizer(sections: [NumberSection]())
    let sections: Driver<[NumberSection]>
    
    init(update: RxCocoa.ControlEvent<()>) {
        // 构建初始数据
        var sectionsData = [NumberSection]()
        for i in 0 ..< 10 {
            sectionsData.append(NumberSection(model: i + 1, items: Array(i ..< i + 100)))
        }
        let generator = Randomizer(sections: sectionsData)
        self.generator = generator
        
        sections = update.map { () -> [NumberSection] in
            generator.randomize()
            return generator.sections
        }.asDriver(onErrorJustReturn: sectionsData)
        .startWith(sectionsData)
    }
}
  • 属性generator用来更新数据,sections就是最终的数据序列
  • 初始化函数中,接收一个更新数据指令的参数update,通过map操作符转换为需要的数据,再使用asDriverstartWith操作符来避免错误以及设置初始元素

绑定数据

回到控制器定义两个懒加载属性tableViewDataSourcecollectionViewDataSource来辅助绑定UITableView和UICollectionView:

    lazy var tableViewDataSource = {
        TableViewSectionedDataSource<NumberSection>(cellForRow: { (ds, tv, ip) -> UITableViewCell in
            let cell = CommonCell.cellFor(tableView: tv)
            cell.textLabel?.text = "\(ds[ip])"
            return cell
        }, titleForHeader: { (ds, tv, i) -> String? in
            return "第\(ds[i].model)组"
        })
    }()
    lazy var collectionViewDataSource = {
        CollectionViewSectionedDataSource<NumberSection>(cellForItem: { [weak self] (ds, cv, ip) -> UICollectionViewCell in
            let cell = TextCollectionViewCell.cellFor(collectionView: cv, indexPath: ip, identifier: self!.cellID)
            cell.textLabel.text = "\(ds[ip])"
            return cell
        }, viewForSupplementaryElement: { [weak self] (ds, cv, kind, ip) -> UICollectionReusableView in
            let view = TextCollectionReusableView.viewFor(collectionView: cv, indexPath: ip, kind: kind, identifier: self!.reusableViewID)
            view.textLabel.text = "第\(ds[ip.section].model)组"
            return view
        })
    }()

在控制器的viewDidLoad函数中将数据绑定到前面搭建的UI元素上,方式都与之前的一样,在此不做分析:

        // 绑定
        let viewModel = UpdatesViewModel(update: rightItem.rx.tap)
        viewModel.sections
            .drive(partialTableView.rx.items(dataSource: tableViewDataSource))
            .disposed(by: bag)
        viewModel.sections
            .drive(tableView.rx.items(dataSource: tableViewDataSource))
            .disposed(by: bag)
        Observable.of(tableView.rx.modelSelected(Int.self), partialTableView.rx.modelSelected(Int.self))
            .merge()
            .subscribe(onNext: {print("我是\($0)") })
            .disposed(by: bag)
        
        viewModel.sections
            .drive(partialCollectionView.rx.items(dataSource: collectionViewDataSource))
            .disposed(by: bag)
        partialCollectionView.rx
            .modelSelected(Int.self)
            .subscribe(onNext: { print("我是\($0)") })
            .disposed(by: bag)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Echarts监控大屏实例十是一个基于Vue框架和Echarts图表库开发的实时监控大屏,用于展示各种实时数据和监控信息。 该实例借助Vue框架的响应式特性,可以实时更新数据并展示在大屏上。通过与后端接口的交互,获取最新的监控数据,并将数据传递给Echarts图表进行可视化展示。 在该实例中,我们可以看到各种类型的图表,如柱状图、折线图、饼状图等,用于展示不同类型的监控指标。例如,柱状图可以展示各个地区的销售额,折线图可以展示系统的响应时间变化趋势,饼状图可以展示各个部门的占比。 除了图表,该实例还提供了其他实用的功能。例如,可以设置数据刷新间隔,使数据可以按照一定的频率进行更新,保持实时性。同时,还可以对图表进行定制化的配置,包括颜色、字体、显示内容等方面,以满足不同的需求。 在实际应用中,该实例可以用于各种监控场景。例如,可以用于电商平台的实时订单监控,可以展示订单量、销售额等信息;也可以用于后台系统的性能监控,可以展示CPU、内存等指标的变化情况。通过将实时数据可视化展示,可以帮助用户快速了解当前状态,并及时采取相应的措施。 总的来说,Vue Echarts监控大屏实例十是一个功能强大、易于使用的实时监控大屏,可以帮助用户实时监控各种指标,并在需要时作出相应的决策。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值