Skip to content

Instantly share code, notes, and snippets.

@sgr-ksmt
Created August 4, 2017 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgr-ksmt/2b00fe0950f579ad3fb76d90acb11fef to your computer and use it in GitHub Desktop.
Save sgr-ksmt/2b00fe0950f579ad3fb76d90acb11fef to your computer and use it in GitHub Desktop.
DataSource
protocol DataSourceSection {
associatedtype Element
var elements: [Element] { get }
}
protocol DataSource {
associatedtype Section: DataSourceSection
var sections: [Section] { get }
}
extension DataSource {
func numberOfSections() -> Int {
return sections.count
}
func numberOfElements(in section: Int) -> Int {
return self[section].elements.count
}
subscript (section: Int) -> Section {
return sections[section]
}
subscript (indexPath: IndexPath) -> Section.Element {
return self[indexPath.section].elements[indexPath.row]
}
}
@sgr-ksmt
Copy link
Author

sgr-ksmt commented Aug 4, 2017

final class SettingDataSource: DataSource {
    struct Section: DataSourceSection {
        enum SectionType {
            case others
        }
        let type: SectionType
        let title: String
        let elements: [SettingDataSource.Row]
    }

    struct Row {
        enum RowType {
            case logout
        }
        let type: RowType
        let title: String
    }

    let sections: [Section]
    init(sections: [Section]) {
        self.sections = sections
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment