Skip to content

Commit 06becd8

Browse files
authored
Merge pull request #62 from pegurov/master
Added support for table section index
2 parents 92b3e3b + c277b65 commit 06becd8

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Sources/TableDirector.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
3131
private weak var scrollDelegate: UIScrollViewDelegate?
3232
private var cellRegisterer: TableCellRegisterer?
3333
public private(set) var rowHeightCalculator: RowHeightCalculator?
34-
34+
private var sectionsIndexTitlesIndexes: [Int]?
35+
3536
@available(*, deprecated, message: "Produced incorrect behaviour")
3637
open var shouldUsePrototypeCellHeightCalculation: Bool = false {
3738
didSet {
@@ -192,6 +193,32 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
192193
return section.footerHeight ?? section.footerView?.frame.size.height ?? UITableViewAutomaticDimension
193194
}
194195

196+
// MARK: UITableViewDataSource - Index
197+
198+
public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
199+
200+
var indexTitles = [String]()
201+
var indexTitlesIndexes = [Int]()
202+
sections.enumerated().forEach { index, section in
203+
204+
if let title = section.indexTitle {
205+
indexTitles.append(title)
206+
indexTitlesIndexes.append(index)
207+
}
208+
}
209+
if !indexTitles.isEmpty {
210+
211+
sectionsIndexTitlesIndexes = indexTitlesIndexes
212+
return indexTitles
213+
}
214+
sectionsIndexTitlesIndexes = nil
215+
return nil
216+
}
217+
218+
public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
219+
return sectionsIndexTitlesIndexes?[index] ?? 0
220+
}
221+
195222
// MARK: UITableViewDelegate - actions
196223

197224
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

Sources/TableSection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ open class TableSection {
2626

2727
open var headerTitle: String?
2828
open var footerTitle: String?
29+
open var indexTitle: String?
2930

3031
open var headerView: UIView?
3132
open var footerView: UIView?

TableKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Pod::Spec.new do |s|
22
s.name = 'TableKit'
33
s.module_name = 'TableKit'
44

5-
s.version = '2.3.1'
5+
s.version = '2.4.0'
66

77
s.homepage = 'https://github.com/maxsokolov/TableKit'
88
s.summary = 'Type-safe declarative table views with Swift.'
@@ -14,4 +14,4 @@ Pod::Spec.new do |s|
1414

1515
s.source_files = 'Sources/*.swift'
1616
s.source = { :git => 'https://github.com/maxsokolov/TableKit.git', :tag => s.version }
17-
end
17+
end

0 commit comments

Comments
 (0)