RxSwift 这个框架看我就够了,这一篇我重点介绍
RxDataSources
在实际开发中的运用,RxDataSources
在tableview
的分组是特别爽的。搞RxSwift
,我是认真的
RxSwift 宝图镇博,咔咔点赞~~~~
-
RxSwift 深入浅出(一)RxSwift初探
-
RxSwift 深入浅出(二)高阶函数
-
RxSwift 深入浅出(三)Subject
-
RxSwift 深入浅出(四)控件应用
-
RxSwift 深入浅出(五)tableView应用
-
RxSwift 深入浅出(六)RxDataSources
-
RxSwift 深入浅出(七)网络封装
-
RxSwift 深入浅出(八)开发总结
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
class RxSwiftTableVC: UIViewController {
var myTableView:UITableView!
let reuserId = "cell"
let disposeB = DisposeBag()
let datas = GithubData()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "RxSwift进阶";
self.view.backgroundColor = UIColor.white
self.myTableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)
self.view.addSubview(self.myTableView)
self.myTableView.register(SectionTableCell.self, forCellReuseIdentifier: reuserId)
let dataSource = RxTableViewSectionedReloadDataSource>(
configureCell: { [weak self](ds, tb, index, model) -> SectionTableCell in
let cell = tb.dequeueReusableCell(withIdentifier: (self?.reuserId)!, for: index) as? SectionTableCell
// cell?.detailTextLabel?.backgroundColor = UIColor.orange
cell?.imageView?.image = model.image
cell?.textLabel?.text = model.name
cell?.detailTextLabel?.text = model.gitHubID
return cell!
},
titleForHeaderInSection:{ds, index in
return ds.sectionModels[index].model
})
datas.githubData.asDriver(onErrorJustReturn: [])
.drive(myTableView.rx.items(dataSource: dataSource))
.disposed(by:disposeB)
myTableView.rx.itemSelected
.subscribe(onNext:{ [weak self] indexPath in
self?.navigationController?.pushViewController(RxSearchVC(), animated: true)
})
.disposed(by:disposeB)
// myTableView.delegate = self
myTableView.rx.setDelegate(self).disposed(by: disposeB)
}
}
class SectionTableCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: reuseIdentifier)
print(#function)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
struct SectionDataModel {
let name: String
let gitHubID: String
var image: UIImage?
init(name: String, gitHubID: String) {
self.name = name
self.gitHubID = gitHubID
image = UIImage(named: gitHubID)
}
}
extension SectionDataModel:IdentifiableType{
typealias Identity = String
var identity:Identity {return gitHubID}
}
class GithubData {
let githubData = Observable.just([
SectionModel(model: "A", items: [
SectionDataModel(name: "Alex V Bush", gitHubID: "alexvbush"),
SectionDataModel(name: "Andrew Breckenridge", gitHubID: "AndrewSB"),
SectionDataModel(name: "Anton Efimenko", gitHubID: "reloni"),
SectionDataModel(name: "Ash Furrow", gitHubID: "ashfurrow"),
]),
SectionModel(model: "B", items: [
SectionDataModel(name: "Ben Emdon", gitHubID: "BenEmdon"),
SectionDataModel(name: "Bob Spryn", gitHubID: "sprynmr"),
]),
SectionModel(model: "C", items: [
SectionDataModel(name: "Carlos García", gitHubID: "carlosypunto"),
SectionDataModel(name: "Cezary Kopacz", gitHubID: "CezaryKopacz"),
SectionDataModel(name: "Chris Jimenez", gitHubID: "PiXeL16"),
SectionDataModel(name: "Christian Tietze", gitHubID: "DivineDominion"),
]),
SectionModel(model: "D", items: [
SectionDataModel(name: "だいちろ", gitHubID: "mokumoku"),
SectionDataModel(name: "David Alejandro", gitHubID: "davidlondono"),
SectionDataModel(name: "David Paschich", gitHubID: "dpassage"),
]),
SectionModel(model: "E", items: [
SectionDataModel(name: "Esteban Torres", gitHubID: "esttorhe"),
SectionDataModel(name: "Evgeny Aleksandrov", gitHubID: "ealeksandrov"),
]),
SectionModel(model: "F", items: [
SectionDataModel(name: "Florent Pillet", gitHubID: "fpillet"),
SectionDataModel(name: "Francis Chong", gitHubID: "siuying"),
]),
SectionModel(model: "G", items: [
SectionDataModel(name: "Giorgos Tsiapaliokas", gitHubID: "terietor"),
SectionDataModel(name: "Guy Kahlon", gitHubID: "GuyKahlon"),
SectionDataModel(name: "Gwendal Roué", gitHubID: "groue"),
]),
SectionModel(model: "H", items: [
SectionDataModel(name: "Hiroshi Kimura", gitHubID: "muukii"),
]),
SectionModel(model: "I", items: [
SectionDataModel(name: "Ivan Bruel", gitHubID: "ivanbruel"),
]),
SectionModel(model: "J", items: [
SectionDataModel(name: "Jeon Suyeol", gitHubID: "devxoul"),
SectionDataModel(name: "Jérôme Alves", gitHubID: "jegnux"),
SectionDataModel(name: "Jesse Farless", gitHubID: "solidcell"),
SectionDataModel(name: "Junior B.", gitHubID: "bontoJR"),
SectionDataModel(name: "Justin Swart", gitHubID: "justinswart"),
]),
SectionModel(model: "K", items: [
SectionDataModel(name: "Karlo", gitHubID: "floskel"),
SectionDataModel(name: "Krunoslav Zaher", gitHubID: "kzaher"),
]),
SectionModel(model: "L", items: [
SectionDataModel(name: "Laurin Brandner", gitHubID: "lbrndnr"),
SectionDataModel(name: "Lee Sun-Hyoup", gitHubID: "kciter"),
SectionDataModel(name: "Leo Picado", gitHubID: "leopic"),
SectionDataModel(name: "Libor Huspenina", gitHubID: "libec"),
SectionDataModel(name: "Lukas Lipka", gitHubID: "lipka"),
SectionDataModel(name: "Łukasz Mróz", gitHubID: "sunshinejr"),
]),
SectionModel(model: "M", items: [
SectionDataModel(name: "Marin Todorov", gitHubID: "icanzilb"),
SectionDataModel(name: "Maurício Hanika", gitHubID: "mAu888"),
SectionDataModel(name: "Maximilian Alexander", gitHubID: "mbalex99"),
]),
SectionModel(model: "N", items: [
SectionDataModel(name: "Nathan Kot", gitHubID: "nathankot"),
]),
SectionModel(model: "O", items: [
SectionDataModel(name: "Orakaro", gitHubID: "DTVD"),
SectionDataModel(name: "Orta", gitHubID: "orta"),
]),
SectionModel(model: "P", items: [
SectionDataModel(name: "Paweł Urbanek", gitHubID: "pawurb"),
SectionDataModel(name: "Pedro Piñera Buendía", gitHubID: "pepibumur"),
SectionDataModel(name: "PG Herveou", gitHubID: "pgherveou"),
]),
SectionModel(model: "R", items: [
SectionDataModel(name: "Rui Costa", gitHubID: "ruipfcosta"),
SectionDataModel(name: "Ryo Fukuda", gitHubID: "yuzushioh"),
]),
SectionModel(model: "S", items: [
SectionDataModel(name: "Scott Gardner", gitHubID: "scotteg"),
SectionDataModel(name: "Scott Hoyt", gitHubID: "scottrhoyt"),
SectionDataModel(name: "Sendy Halim", gitHubID: "sendyhalim"),
SectionDataModel(name: "Serg Dort", gitHubID: "sergdort"),
SectionDataModel(name: "Shai Mishali", gitHubID: "freak4pc"),
SectionDataModel(name: "Shams Ahmed", gitHubID: "shams-ahmed"),
SectionDataModel(name: "Shenghan Chen", gitHubID: "zzdjk6"),
SectionDataModel(name: "Shunki Tan", gitHubID: "milkit"),
SectionDataModel(name: "Spiros Gerokostas", gitHubID: "sger"),
SectionDataModel(name: "Stefano Mondino", gitHubID: "stefanomondino"),
]),
SectionModel(model: "T", items: [
SectionDataModel(name: "Thane Gill", gitHubID: "thanegill"),
SectionDataModel(name: "Thomas Duplomb", gitHubID: "tomahh"),
SectionDataModel(name: "Tomasz Pikć", gitHubID: "pikciu"),
SectionDataModel(name: "Tony Arnold", gitHubID: "tonyarnold"),
SectionDataModel(name: "Torsten Curdt", gitHubID: "tcurdt"),
]),
SectionModel(model: "V", items: [
SectionDataModel(name: "Vladimir Burdukov", gitHubID: "chipp"),
]),
SectionModel(model: "W", items: [
SectionDataModel(name: "Wolfgang Lutz", gitHubID: "Lutzifer"),
]),
SectionModel(model: "X", items: [
SectionDataModel(name: "xixi197 Nova", gitHubID: "xixi197"),
]),
SectionModel(model: "Y", items: [
SectionDataModel(name: "Yongha Yoo", gitHubID: "inkyfox"),
SectionDataModel(name: "Yosuke Ishikawa", gitHubID: "ishkawa"),
SectionDataModel(name: "Yury Korolev", gitHubID: "yury"),
]),
])
}
extension RxSwiftTableVC:UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(arc4random_uniform(60)+32)
}
}
RxDataSources
的分组效果是非常爽的,我们现在看一下它的封装
public typealias ConfigureCell = (TableViewSectionedDataSource, UITableView, IndexPath, I) -> UITableViewCell
public typealias TitleForHeaderInSection = (TableViewSectionedDataSource, Int) -> String?
public typealias TitleForFooterInSection = (TableViewSectionedDataSource, Int) -> String?
public typealias CanEditRowAtIndexPath = (TableViewSectionedDataSource, IndexPath) -> Bool
public typealias CanMoveRowAtIndexPath = (TableViewSectionedDataSource, IndexPath) -> Bool
定义了几个闭包,也是我们在分组中经常要用到的
public init(
configureCell: @escaping ConfigureCell,
titleForHeaderInSection: @escaping TitleForHeaderInSection = { _, _ in nil },
titleForFooterInSection: @escaping TitleForFooterInSection = { _, _ in nil },
canEditRowAtIndexPath: @escaping CanEditRowAtIndexPath = { _, _ in false },
canMoveRowAtIndexPath: @escaping CanMoveRowAtIndexPath = { _, _ in false },
sectionIndexTitles: @escaping SectionIndexTitles = { _ in nil },
sectionForSectionIndexTitle: @escaping SectionForSectionIndexTitle = { _, _, index in index }
) {
self.configureCell = configureCell
self.titleForHeaderInSection = titleForHeaderInSection
self.titleForFooterInSection = titleForFooterInSection
self.canEditRowAtIndexPath = canEditRowAtIndexPath
self.canMoveRowAtIndexPath = canMoveRowAtIndexPath
self.sectionIndexTitles = sectionIndexTitles
self.sectionForSectionIndexTitle = sectionForSectionIndexTitle
}
在初始化的时候就对上面的闭包进行了默认值处理(除了configureCell),这也很好解释,因为对于分组来说,其他那些头,尾都是非必须的,cell的逻辑才是必须要的,这也就促成了它封装两个
configureCell
的方法的原因