Swift4 RxSwift RxDataSources UITableView基本使用 单Cell

自定义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?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.tableFooterView = UIView()
        
        let dataSource = RxTableViewSectionedAnimatedDataSource(
            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 {

}


Swift4 RxSwift RxDataSources UITableView基本使用 单Cell_第1张图片
Simulator Screen Shot - iPhone 8 - 2018-01-01 at 09.47.29.png

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

参考

Swift4 RxSwift RxDataSources UITableView简单使用

你可能感兴趣的:(Swift4 RxSwift RxDataSources UITableView基本使用 单Cell)