美文网首页ios developers
Swift4 RxSwift RxDataSources UIT

Swift4 RxSwift RxDataSources UIT

作者: 巴糖 | 来源:发表于2017-12-29 17:00 被阅读1195次
    import Foundation
    import UIKit
    
    import RxSwift
    import RxCocoa
    import RxDataSources
    import Differentiator
    
    struct MySection {
        var header: String
        var items: [Item]
    }
    
    extension MySection : AnimatableSectionModelType {
        typealias Item = Int
        
        var identity: String {
            return header
        }
        
        init(original: MySection, items: [Item]) {
            self = original
            self.items = items
        }
    }
    
    class BJTableViewSimpleViewController : UIViewController {
        @IBOutlet var tableView: UITableView!
        
        let disposeBag = DisposeBag()
        
        var dataSource: RxTableViewSectionedAnimatedDataSource<MySection>?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
            
            let dataSource = RxTableViewSectionedAnimatedDataSource<MySection>(
                configureCell: { ds, tv, ip, item in
                    let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
                    cell.textLabel?.text = "Item \(item)"
                    
                    return cell
            },
                titleForHeaderInSection: { ds, index in
                    return ds.sectionModels[index].header
            }
            )
            
            self.dataSource = dataSource
            
            let sections = [
                MySection(header: "First section", items: [
                    1,
                    2
                    ]),
                MySection(header: "Second section", items: [
                    3,
                    4
                    ])
            ]
            
            Observable.just(sections)
                .bind(to: tableView.rx.items(dataSource: dataSource))
                .disposed(by: disposeBag)
            
            tableView.rx.setDelegate(self)
                .disposed(by: disposeBag)
        }
    }
    
    extension BJTableViewSimpleViewController : UITableViewDelegate {
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            
            // you can also fetch item
            guard let item = dataSource?[indexPath],
                // .. or section and customize what you like
                let _ = dataSource?[indexPath.section]
                else {
                    return 0.0
            }
            
            return CGFloat(40 + item * 10)
        }
    }
    
    
    基本使用

    注:如自定xib,请注册cell。storyboard中可以省略注册cell。

    相关文章

      网友评论

        本文标题:Swift4 RxSwift RxDataSources UIT

        本文链接:https://www.haomeiwen.com/subject/egxwgxtx.html