//
// ViewController.swift
// RxSwiftTest
//
// Created by xiaoP on 2017/3/15.
// Copyright © 2017年 Chicv. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
import SnapKit
import RxGesture
import RxOptional
import RxDataSources
class ViewController: UIViewController {
let tableView = UITableView(frame: CGRect.zero)
let dataSource = RxTableViewSectionedAnimatedDataSource<PriceSectionModel>()
let bag = DisposeBag()
fileprivate var heightConstraint: Constraint?
override func viewDidLoad() {
super.viewDidLoad()
configUI()
bind()
}
fileprivate func configUI() {
automaticallyAdjustsScrollViewInsets = false
let rightItem = UIBarButtonItem(title: "insert/delete", style: .done, target: nil, action: nil)
navigationItem.rightBarButtonItem = rightItem
view.backgroundColor = UIColor.black
view.addSubview(tableView)
tableView.snp.makeConstraints { (make: ConstraintMaker) in
make.left.right.bottom.equalToSuperview()
self.heightConstraint = make.height.equalTo(0).constraint
}
tableView.delegate = self
tableView.tableFooterView = UIView()
tableView.register(UINib(nibName: "PriceCell", bundle: nil), forCellReuseIdentifier: "PriceCell")
tableView.register(UINib(nibName: "PayCell", bundle: nil), forCellReuseIdentifier: "PayCell")
dataSource.configureCell = { (ds, tv, ip, item) in
switch item {
case let .price(title, value):
let cell = tv.dequeueReusableCell(withIdentifier: "PriceCell", for: ip) as! PriceCell
cell.fillData(title: title, value: value)
return cell
case .pay:
let cell = tv.dequeueReusableCell(withIdentifier: "PayCell", for: ip) as! PayCell
return cell
}
}
}
fileprivate func bind() {
let items: Variable<[PriceItem]> = Variable([
.price(title: "Sub Total", value: "$135.50"),
.price(title: "Quantity", value: "6"),
.price(title: "Standard Delivery", value: "$10.00"),
.price(title: "Total", value: "$145.50"),
.pay
])
let rightItem = navigationItem.rightBarButtonItem!
rightItem.rx.tap
.map{ _-> PriceItem in
let count = Int(arc4random() % 1000)
return .price(title: "Inserted\(count)", value: "$0.00")
}
.bindNext { (item: PriceItem) in
let index = Int(arc4random() % UInt32(items.value.count))
if index % 2 == 0 && items.value.isNotEmpty {
items.value.remove(at: index)
} else {
items.value.insert(item, at: index)
}
}
.disposed(by: bag)
let data = items
.asObservable()
.map { (items: [PriceItem]) -> [PriceSectionModel] in
return [PriceSectionModel(header: "", prices: items)]
}
data
.delay(1.5, scheduler: MainScheduler.instance)
.bindTo(tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
data
.map { (sections: [PriceSectionModel]) -> CGFloat in
return sections.reduce(0){ (sum, section) in
return sum + section.items.reduce(0){ (sum, item) in
switch item {
case .price:
return sum + 44
case .pay:
return sum + 50
}
}
}
}
.bindNext { [weak self] (height: CGFloat) in
self?.heightConstraint?.update(offset: height)
UIView.animate(withDuration: 1, animations: { [weak self] in
self?.view.layoutIfNeeded()
})
}
.disposed(by: bag)
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let item = dataSource[indexPath]
switch item {
case .price:
return 44
case .pay:
return 50
}
}
}
struct PriceSectionModel {
var header: String
var prices: [PriceItem]
init(header: String, prices: [Item]) {
self.header = header
self.prices = prices
}
}
extension PriceSectionModel: AnimatableSectionModelType {
typealias Item = PriceItem
typealias Identity = String
var identity: String {
return header
}
var items: [Item] {
return prices
}
init(original: PriceSectionModel, items: [Item]) {
self = original
self.prices = items
}
}
enum PriceItem: IdentifiableType, Equatable {
typealias Identity = String
case price(title: String, value: String)
case pay
var identity: String {
switch self {
case let .price(title, _):
return "price-\(title)"
case .pay:
return "pay"
}
}
static func ==(l: PriceItem, r: PriceItem)-> Bool {
return l.identity == r.identity
}
}
RxDataSource 动画数据源
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 你是否遇到过这样的需求,在tableview中显示一列数据,点击某一个cell时,在此cell下显示相应的附加信息...
- 配置文件方式,这里使用YMAL,你也可以使用传统的Properties,只要将:更换为.的形式即可。 创建Dyna...