From b49b01ff599a58b29dc4638ab2e1e42532333659 Mon Sep 17 00:00:00 2001 From: Pavel Gurov Date: Thu, 13 Apr 2017 10:52:29 +0300 Subject: [PATCH 1/3] Added support for table section index --- Sources/TableDirector.swift | 16 +++++++++++++++- TableKit.podspec | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 808d4be..925aeb0 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -31,7 +31,9 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { private weak var scrollDelegate: UIScrollViewDelegate? private var cellRegisterer: TableCellRegisterer? public private(set) var rowHeightCalculator: RowHeightCalculator? - + public var sectionIndexTitles: [String]? + public var onSectionForIndex: ((_ title: String, _ index: Int) -> Int)? + @available(*, deprecated, message: "Produced incorrect behaviour") open var shouldUsePrototypeCellHeightCalculation: Bool = false { didSet { @@ -192,6 +194,18 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { return section.footerHeight ?? section.footerView?.frame.size.height ?? UITableViewAutomaticDimension } + // MARK: UITableViewDataSource - Index + + public func sectionIndexTitles(for tableView: UITableView) -> [String]? { + return sectionIndexTitles + } + + public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { + guard let onSectionForIndex = onSectionForIndex else { return 0 } + + return onSectionForIndex(title, index) + } + // MARK: UITableViewDelegate - actions open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { diff --git a/TableKit.podspec b/TableKit.podspec index 22d3c4c..260e2cc 100644 --- a/TableKit.podspec +++ b/TableKit.podspec @@ -2,7 +2,7 @@ Pod::Spec.new do |s| s.name = 'TableKit' s.module_name = 'TableKit' - s.version = '2.3.1' + s.version = '2.4.0' s.homepage = 'https://github.com/maxsokolov/TableKit' s.summary = 'Type-safe declarative table views with Swift.' @@ -14,4 +14,4 @@ Pod::Spec.new do |s| s.source_files = 'Sources/*.swift' s.source = { :git => 'https://github.com/maxsokolov/TableKit.git', :tag => s.version } -end \ No newline at end of file +end From 39b4df6ee92dd4e25ee7e0cbc52a797ca835b558 Mon Sep 17 00:00:00 2001 From: Pavel Gurov Date: Thu, 13 Apr 2017 11:37:08 +0300 Subject: [PATCH 2/3] moved indexTitle to TableSection --- Sources/TableDirector.swift | 19 +++++++++++++------ Sources/TableSection.swift | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 925aeb0..3d6a175 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -31,8 +31,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { private weak var scrollDelegate: UIScrollViewDelegate? private var cellRegisterer: TableCellRegisterer? public private(set) var rowHeightCalculator: RowHeightCalculator? - public var sectionIndexTitles: [String]? - public var onSectionForIndex: ((_ title: String, _ index: Int) -> Int)? + private var sectionsIndex = [Int]() @available(*, deprecated, message: "Produced incorrect behaviour") open var shouldUsePrototypeCellHeightCalculation: Bool = false { @@ -197,13 +196,21 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { // MARK: UITableViewDataSource - Index public func sectionIndexTitles(for tableView: UITableView) -> [String]? { - return sectionIndexTitles + + var indexTitles = [String]() + sectionsIndex = [] + sections.enumerated().forEach { index, section in + + if let title = section.indexTitle { + indexTitles.append(title) + sectionsIndex.append(index) + } + } + return indexTitles.isEmpty ? nil : indexTitles } public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { - guard let onSectionForIndex = onSectionForIndex else { return 0 } - - return onSectionForIndex(title, index) + return sectionsIndex[index] } // MARK: UITableViewDelegate - actions diff --git a/Sources/TableSection.swift b/Sources/TableSection.swift index bc50522..ddc93e7 100644 --- a/Sources/TableSection.swift +++ b/Sources/TableSection.swift @@ -26,6 +26,7 @@ open class TableSection { open var headerTitle: String? open var footerTitle: String? + open var indexTitle: String? open var headerView: UIView? open var footerView: UIView? From c277b6529c6e5a6f3c9894556f32ed99548bead5 Mon Sep 17 00:00:00 2001 From: Pavel Gurov Date: Thu, 13 Apr 2017 14:13:44 +0300 Subject: [PATCH 3/3] Renamed to sectionsIndexTitlesIndexes, and it is now nil if not used --- Sources/TableDirector.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 3d6a175..c251dc7 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -31,7 +31,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { private weak var scrollDelegate: UIScrollViewDelegate? private var cellRegisterer: TableCellRegisterer? public private(set) var rowHeightCalculator: RowHeightCalculator? - private var sectionsIndex = [Int]() + private var sectionsIndexTitlesIndexes: [Int]? @available(*, deprecated, message: "Produced incorrect behaviour") open var shouldUsePrototypeCellHeightCalculation: Bool = false { @@ -198,19 +198,25 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { public func sectionIndexTitles(for tableView: UITableView) -> [String]? { var indexTitles = [String]() - sectionsIndex = [] + var indexTitlesIndexes = [Int]() sections.enumerated().forEach { index, section in if let title = section.indexTitle { indexTitles.append(title) - sectionsIndex.append(index) + indexTitlesIndexes.append(index) } } - return indexTitles.isEmpty ? nil : indexTitles + if !indexTitles.isEmpty { + + sectionsIndexTitlesIndexes = indexTitlesIndexes + return indexTitles + } + sectionsIndexTitlesIndexes = nil + return nil } public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { - return sectionsIndex[index] + return sectionsIndexTitlesIndexes?[index] ?? 0 } // MARK: UITableViewDelegate - actions