Skip to content

Commit 83c4220

Browse files
committed
Merge branch 'releases/0.4'
2 parents 180530b + 164ba2e commit 83c4220

24 files changed

+795
-474
lines changed

Example/MYTableViewIndex.xcodeproj/project.pbxproj

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
0F65EE011D0C8B45002F0E84 /* IndexDataSources.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F65EDFC1D0C8B45002F0E84 /* IndexDataSources.swift */; };
1414
0F65EE031D0C8C1D002F0E84 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0F65EE021D0C8C1D002F0E84 /* Assets.xcassets */; };
1515
0F65EE051D0C8C63002F0E84 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0F65EE041D0C8C63002F0E84 /* Main.storyboard */; };
16-
0FA2C1BB1D313FC200882C6E /* Cell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FA2C1BA1D313FC200882C6E /* Cell.swift */; };
16+
0F9FD75B1EFE83FF001D855C /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F9FD75A1EFE83FF001D855C /* CollectionViewController.swift */; };
17+
0FA2C1BB1D313FC200882C6E /* Cells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FA2C1BA1D313FC200882C6E /* Cells.swift */; };
1718
0FA2C1BD1D313FCF00882C6E /* DataSources.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FA2C1BC1D313FCF00882C6E /* DataSources.swift */; };
1819
0FA2C1BF1D31702D00882C6E /* Examples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FA2C1BE1D31702D00882C6E /* Examples.swift */; };
1920
0FA2C1C11D31806600882C6E /* ExamplesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FA2C1C01D31806600882C6E /* ExamplesViewController.swift */; };
@@ -39,7 +40,8 @@
3940
0F65EDFC1D0C8B45002F0E84 /* IndexDataSources.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexDataSources.swift; sourceTree = "<group>"; };
4041
0F65EE021D0C8C1D002F0E84 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4142
0F65EE041D0C8C63002F0E84 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
42-
0FA2C1BA1D313FC200882C6E /* Cell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cell.swift; sourceTree = "<group>"; };
43+
0F9FD75A1EFE83FF001D855C /* CollectionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = "<group>"; };
44+
0FA2C1BA1D313FC200882C6E /* Cells.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cells.swift; sourceTree = "<group>"; };
4345
0FA2C1BC1D313FCF00882C6E /* DataSources.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataSources.swift; sourceTree = "<group>"; };
4446
0FA2C1BE1D31702D00882C6E /* Examples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Examples.swift; sourceTree = "<group>"; };
4547
0FA2C1C01D31806600882C6E /* ExamplesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExamplesViewController.swift; sourceTree = "<group>"; };
@@ -116,11 +118,12 @@
116118
children = (
117119
0FA2C1C01D31806600882C6E /* ExamplesViewController.swift */,
118120
0F65EDFA1D0C8B45002F0E84 /* TableViewController.swift */,
121+
0F9FD75A1EFE83FF001D855C /* CollectionViewController.swift */,
119122
0F65EDF91D0C8B45002F0E84 /* ViewController.swift */,
120123
0FA2C1BE1D31702D00882C6E /* Examples.swift */,
121124
0FA2C1BC1D313FCF00882C6E /* DataSources.swift */,
122125
0F65EDFC1D0C8B45002F0E84 /* IndexDataSources.swift */,
123-
0FA2C1BA1D313FC200882C6E /* Cell.swift */,
126+
0FA2C1BA1D313FC200882C6E /* Cells.swift */,
124127
0F65EDF81D0C8B45002F0E84 /* AppDelegate.swift */,
125128
0F65EE041D0C8C63002F0E84 /* Main.storyboard */,
126129
0F65EE021D0C8C1D002F0E84 /* Assets.xcassets */,
@@ -376,8 +379,9 @@
376379
isa = PBXSourcesBuildPhase;
377380
buildActionMask = 2147483647;
378381
files = (
382+
0F9FD75B1EFE83FF001D855C /* CollectionViewController.swift in Sources */,
379383
0F65EDFE1D0C8B45002F0E84 /* ViewController.swift in Sources */,
380-
0FA2C1BB1D313FC200882C6E /* Cell.swift in Sources */,
384+
0FA2C1BB1D313FC200882C6E /* Cells.swift in Sources */,
381385
0F65EDFF1D0C8B45002F0E84 /* TableViewController.swift in Sources */,
382386
0FA2C1BD1D313FCF00882C6E /* DataSources.swift in Sources */,
383387
0F65EDFD1D0C8B45002F0E84 /* AppDelegate.swift in Sources */,

Example/MYTableViewIndex/Cell.swift Example/MYTableViewIndex/Cells.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
class Cell : UITableViewCell {
11+
class TableCell : UITableViewCell {
1212

1313
func setupWithItem(_ item: Item) {
1414
textLabel?.text = item as? String
@@ -19,3 +19,12 @@ class Cell : UITableViewCell {
1919
return item is UIColor ? 100 : 44
2020
}
2121
}
22+
23+
class CollectionCell : UICollectionViewCell {
24+
25+
@IBOutlet weak var textLabel: UILabel!
26+
27+
func setupWithItem(_ item: Item) {
28+
textLabel?.text = item as? String
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// CollectionViewController.swift
3+
// MYTableViewIndex
4+
//
5+
// Created by Makarov Yury on 24/06/2017.
6+
// Copyright © 2017 Makarov Yury. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import MYTableViewIndex
11+
12+
class CollectionHeader : UICollectionReusableView {
13+
14+
@IBOutlet weak var titleLabel: UILabel!
15+
}
16+
17+
class CollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout, TableViewIndexDelegate, ExampleContainer {
18+
19+
var example: Example!
20+
21+
fileprivate var dataSource: DataSource!
22+
23+
fileprivate var tableViewIndexController: TableViewIndexController!
24+
25+
override func viewDidLoad() {
26+
super.viewDidLoad()
27+
28+
dataSource = example.dataSource
29+
30+
tableViewIndexController = TableViewIndexController(scrollView: collectionView!)
31+
tableViewIndexController.tableViewIndex.delegate = self
32+
33+
example.setupTableIndexController(tableViewIndexController)
34+
35+
let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
36+
layout.headerReferenceSize = CGSize(width: view.bounds.size.width, height: 30)
37+
layout.itemSize = CGSize(width: view.bounds.size.width, height: 44)
38+
}
39+
40+
// MARK: - UICollectionView
41+
42+
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
43+
return dataSource.numberOfItemsInSection(section)
44+
}
45+
46+
override func numberOfSections(in collectionView: UICollectionView) -> Int {
47+
return dataSource.numberOfSections()
48+
}
49+
50+
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
51+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
52+
if let item = dataSource.itemAtIndexPath(indexPath) {
53+
cell.setupWithItem(item)
54+
}
55+
return cell
56+
}
57+
58+
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
59+
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! CollectionHeader
60+
header.titleLabel.text = dataSource.titleForHeaderInSection(indexPath.section)
61+
return header
62+
}
63+
64+
// MARK: - TableViewIndex
65+
66+
func tableViewIndex(_ tableViewIndex: TableViewIndex, didSelect item: UIView, at index: Int) -> Bool {
67+
let sectionIndex = example.mapIndexItemToSection(item, index: index)
68+
if sectionIndex == NSNotFound {
69+
return false
70+
}
71+
let indexPath = IndexPath(row: 0, section: sectionIndex)
72+
guard let attrs = collectionView!.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) else {
73+
return false
74+
}
75+
let yOffset = min(attrs.frame.origin.y, collectionView!.contentSize.height - collectionView!.frame.height + collectionView!.contentInset.top)
76+
collectionView!.contentOffset = CGPoint(x: 0, y: yOffset - collectionView!.contentInset.top)
77+
78+
return true
79+
}
80+
}

Example/MYTableViewIndex/Examples.swift

+24-19
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import UIKit
1010
import MYTableViewIndex
1111

1212
enum ExampleType : String {
13-
case comparison = "comparison"
14-
case customBackground = "background"
15-
case hidingIndex = "autohide"
16-
case coloredIndex = "color"
17-
case imageIndex = "image"
18-
case largeFont = "large"
13+
case comparison
14+
case customBackground
15+
case hidingIndex
16+
case coloredIndex
17+
case imageIndex
18+
case largeFont
19+
case collectionView
1920
}
2021

2122
protocol Example {
@@ -33,7 +34,6 @@ protocol Example {
3334
func trackSelectedSections(_ sections: Set<Int>)
3435
}
3536

36-
3737
class BasicExample : Example {
3838

3939
var dataSource: DataSource {
@@ -45,7 +45,7 @@ class BasicExample : Example {
4545
}
4646

4747
var hasSearchIndex: Bool {
48-
return true
48+
return false
4949
}
5050

5151
func mapIndexItemToSection(_ indexItem: IndexItem, index: NSInteger) -> Int {
@@ -59,6 +59,12 @@ class BasicExample : Example {
5959
func trackSelectedSections(_ sections: Set<Int>) {}
6060
}
6161

62+
class SearchExample : BasicExample {
63+
64+
override var hasSearchIndex: Bool {
65+
return true
66+
}
67+
}
6268

6369
class BackgroundView : UIView {
6470

@@ -104,10 +110,15 @@ class CustomBackgroundExample : BasicExample {
104110

105111
tableIndexController.tableViewIndex.backgroundView = backgroundView
106112
tableIndexController.tableViewIndex.indexInset = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
113+
tableIndexController.tableViewIndex.indexOffset = UIOffset()
107114

108115
tableIndexController.layouter = { tableView, tableIndex in
109116
var frame = tableIndex.frame
110-
frame.origin = CGPoint(x: frame.origin.x - 3, y: frame.origin.y)
117+
if (UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft) {
118+
frame.origin = CGPoint(x: frame.origin.x + 3, y: frame.origin.y)
119+
} else {
120+
frame.origin = CGPoint(x: frame.origin.x - 3, y: frame.origin.y)
121+
}
111122
tableIndex.frame = frame
112123
};
113124

@@ -132,13 +143,9 @@ class CustomBackgroundExample : BasicExample {
132143
}
133144
})
134145
}
135-
136-
override var hasSearchIndex: Bool {
137-
return false
138-
}
139146
}
140147

141-
class LargeFontExample : BasicExample {
148+
class LargeFontExample : SearchExample {
142149

143150
override func setupTableIndexController(_ tableIndexController: TableViewIndexController) {
144151
super.setupTableIndexController(tableIndexController)
@@ -158,7 +165,7 @@ class AutohidingIndexExample : BasicExample {
158165
}
159166
}
160167

161-
class ColoredIndexExample : BasicExample {
168+
class ColoredIndexExample : SearchExample {
162169

163170
override var indexDataSource: TableViewIndexDataSource {
164171
return ColoredIndexDataSource()
@@ -167,10 +174,6 @@ class ColoredIndexExample : BasicExample {
167174

168175
class ImageIndexExample : BasicExample {
169176

170-
override var hasSearchIndex: Bool {
171-
return false
172-
}
173-
174177
override var indexDataSource: TableViewIndexDataSource {
175178
return ImageIndexDataSource()
176179
}
@@ -193,6 +196,8 @@ func exampleByType(_ type: ExampleType) -> Example {
193196
case .largeFont:
194197
return LargeFontExample()
195198
case .comparison:
199+
return SearchExample()
200+
case .collectionView:
196201
return BasicExample()
197202
}
198203
}

0 commit comments

Comments
 (0)