美文网首页
Swift4 RxSwift RxDataSources UIT

Swift4 RxSwift RxDataSources UIT

作者: 巴糖 | 来源:发表于2018-01-01 09:48 被阅读571次

    自定义Cell

    import UIKit
    
    class BJTitleTableViewCell: UITableViewCell {
    
        @IBOutlet weak var lblTitle: UILabel!
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    
    }
    

    BJTableViewBasicViewController实现

    import RxSwift
    import RxCocoa
    import RxDataSources
    import Differentiator
    
    struct BJTitleSection {
        var header: String
        var items: [Item]
    }
    
    extension BJTitleSection : AnimatableSectionModelType {
        // 可以切换成模型
        typealias Item = String
        
        var identity: String {
            return header
        }
        
        init(original: BJTitleSection, items: [Item]) {
            self = original
            self.items = items
        }
    }
    
    class BJTableViewBasicViewController : UIViewController {
        @IBOutlet var tableView: UITableView!
        
        let disposeBag = DisposeBag()
        
        var dataSource: RxTableViewSectionedAnimatedDataSource<BJTitleSection>?
        
        override func viewDidLoad() {
            super.viewDidLoad()
        
            tableView.tableFooterView = UIView()
            
            let dataSource = RxTableViewSectionedAnimatedDataSource<BJTitleSection>(
                configureCell: { ds, tv, ip, item in
                    let cell = tv.dequeueReusableCell(withIdentifier: "BJTitleTableViewCell", for: ip) as! BJTitleTableViewCell
                    cell.lblTitle.text = ds.sectionModels[ip.section].items[ip.row]
                    
                    return cell
            })
            
            self.dataSource = dataSource
            
            let sections = [
                BJTitleSection(header: "UITableView", items: [
                    "女人三字经:","死远些","别碰我","放开手","我喊了","你讨厌","不可以","不要嘛","你轻点","好舒服","不要停","用力点","不行了","抱紧我","要来了","快咬我","我还要","…………"
                    ])
                
            ]
            
            Observable.just(sections)
                .bind(to: tableView.rx.items(dataSource: dataSource))
                .disposed(by: disposeBag)
            
            tableView.rx.setDelegate(self)
                .disposed(by: disposeBag)
        }
    }
    
    // MARK: - UITableViewDelegate 必须写
    extension BJTableViewBasicViewController : UITableViewDelegate {
    
    }
    
    
    
    Simulator Screen Shot - iPhone 8 - 2018-01-01 at 09.47.29.png

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

    参考

    Swift4 RxSwift RxDataSources UITableView简单使用

    相关文章

      网友评论

          本文标题:Swift4 RxSwift RxDataSources UIT

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