Created
November 29, 2016 14:53
-
-
Save MylesCaley/a2c9ae44d338f6b8b8aefeb3416ec0d6 to your computer and use it in GitHub Desktop.
Creating a dynamic header with multiple sections and multiple items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//section model file | |
import RxDataSources | |
enum CommentTableSectionModel { | |
case section(comment: Comment, items: [CommentTableSectionModelItem]) | |
} | |
extension CommentTableSectionModel: SectionModelType { | |
// define the different types of items that can go in the feed here | |
enum CommentTableSectionModelItem { | |
case commentSectionItem(Comment) | |
} | |
var items: [CommentTableSectionModelItem] { | |
switch self { | |
case .section(comment: _, items: let items): | |
return items | |
} | |
} | |
var comment: Comment { | |
switch self { | |
case .section(comment: let comment, items: _): | |
return comment | |
} | |
} | |
init(original: CommentTableSectionModel, items: [CommentTableSectionModelItem]) { | |
switch original { | |
case let .section(comment: comment, items: _): | |
self = .section(comment: comment, items: items) | |
} | |
} | |
} | |
// to drive the table view... | |
refreshControl.rx.controlEvent(.valueChanged) | |
.startWith(()) | |
.flatMapLatest { _ in | |
return items | |
} | |
.map { items -> [CommentTableSectionModel] in | |
return items.map { item in | |
return CommentTableSectionModel.section(comment: item as! Comment, items: | |
items.map{ | |
return CommentTableSectionModel.CommentTableSectionModelItem.commentSectionItem($0 as! Comment) | |
} | |
) | |
} | |
} | |
.asDriver(onErrorJustReturn: []) | |
.drive((tableView?.rx.items(dataSource: dataSource))!) | |
.addDisposableTo(rx_disposeBag) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment