Skip to content

Commit af34834

Browse files
authored
Merge pull request #9 from hryk224/fix/remove_cellwidth
Removed `var cellWidth`
2 parents 1c9d9ca + b82efbc commit af34834

File tree

12 files changed

+107
-86
lines changed

12 files changed

+107
-86
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ DerivedData
2020
*.ipa
2121
*.app
2222

23-
*.xccheckout
23+
*.xccheckout
24+
sort-Xcode-project-file

Example/Example.xcodeproj/project.pbxproj

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
isa = PBXGroup;
8787
children = (
8888
935BD6C71CE26EB600E5FDF7 /* Example */,
89-
935BD6C61CE26EB600E5FDF7 /* Products */,
9089
EDA05AD11D92901900F7C2C5 /* Frameworks */,
90+
935BD6C61CE26EB600E5FDF7 /* Products */,
9191
);
9292
sourceTree = "<group>";
9393
};
@@ -102,20 +102,36 @@
102102
935BD6C71CE26EB600E5FDF7 /* Example */ = {
103103
isa = PBXGroup;
104104
children = (
105+
ED510E6D1DDD513200470C8B /* Pattern1 */,
106+
ED510E6E1DDD513F00470C8B /* Pattern2 */,
105107
935BD6C81CE26EB600E5FDF7 /* AppDelegate.swift */,
108+
935BD6CF1CE26EB600E5FDF7 /* Assets.xcassets */,
106109
935BD6DA1CE26F0B00E5FDF7 /* ImageCollectionViewCell.swift */,
107110
935BD6DB1CE26F0B00E5FDF7 /* ImageCollectionViewCell.xib */,
111+
935BD6D41CE26EB600E5FDF7 /* Info.plist */,
112+
935BD6D11CE26EB600E5FDF7 /* LaunchScreen.storyboard */,
113+
935BD6CC1CE26EB600E5FDF7 /* Main.storyboard */,
108114
935BD6DC1CE26F0B00E5FDF7 /* MainViewController.swift */,
115+
);
116+
path = Example;
117+
sourceTree = "<group>";
118+
};
119+
ED510E6D1DDD513200470C8B /* Pattern1 */ = {
120+
isa = PBXGroup;
121+
children = (
109122
935BD6DD1CE26F0B00E5FDF7 /* Pattern1.storyboard */,
110123
935BD6DE1CE26F0B00E5FDF7 /* Pattern1ViewController.swift */,
124+
);
125+
name = Pattern1;
126+
sourceTree = "<group>";
127+
};
128+
ED510E6E1DDD513F00470C8B /* Pattern2 */ = {
129+
isa = PBXGroup;
130+
children = (
111131
935BD6DF1CE26F0B00E5FDF7 /* Pattern2.storyboard */,
112132
935BD6E01CE26F0B00E5FDF7 /* Pattern2ViewController.swift */,
113-
935BD6CC1CE26EB600E5FDF7 /* Main.storyboard */,
114-
935BD6CF1CE26EB600E5FDF7 /* Assets.xcassets */,
115-
935BD6D11CE26EB600E5FDF7 /* LaunchScreen.storyboard */,
116-
935BD6D41CE26EB600E5FDF7 /* Info.plist */,
117133
);
118-
path = Example;
134+
name = Pattern2;
119135
sourceTree = "<group>";
120136
};
121137
EDA05AD11D92901900F7C2C5 /* Frameworks */ = {
@@ -211,12 +227,12 @@
211227
isa = PBXResourcesBuildPhase;
212228
buildActionMask = 2147483647;
213229
files = (
214-
935BD6E61CE26F0B00E5FDF7 /* Pattern2.storyboard in Resources */,
215-
935BD6D31CE26EB600E5FDF7 /* LaunchScreen.storyboard in Resources */,
216230
935BD6D01CE26EB600E5FDF7 /* Assets.xcassets in Resources */,
217-
935BD6E41CE26F0B00E5FDF7 /* Pattern1.storyboard in Resources */,
218231
935BD6E21CE26F0B00E5FDF7 /* ImageCollectionViewCell.xib in Resources */,
232+
935BD6D31CE26EB600E5FDF7 /* LaunchScreen.storyboard in Resources */,
219233
935BD6CE1CE26EB600E5FDF7 /* Main.storyboard in Resources */,
234+
935BD6E41CE26F0B00E5FDF7 /* Pattern1.storyboard in Resources */,
235+
935BD6E61CE26F0B00E5FDF7 /* Pattern2.storyboard in Resources */,
220236
);
221237
runOnlyForDeploymentPostprocessing = 0;
222238
};
@@ -227,11 +243,11 @@
227243
isa = PBXSourcesBuildPhase;
228244
buildActionMask = 2147483647;
229245
files = (
246+
935BD6C91CE26EB600E5FDF7 /* AppDelegate.swift in Sources */,
247+
935BD6E11CE26F0B00E5FDF7 /* ImageCollectionViewCell.swift in Sources */,
230248
935BD6E31CE26F0B00E5FDF7 /* MainViewController.swift in Sources */,
231249
935BD6E51CE26F0B00E5FDF7 /* Pattern1ViewController.swift in Sources */,
232-
935BD6E11CE26F0B00E5FDF7 /* ImageCollectionViewCell.swift in Sources */,
233250
935BD6E71CE26F0B00E5FDF7 /* Pattern2ViewController.swift in Sources */,
234-
935BD6C91CE26EB600E5FDF7 /* AppDelegate.swift in Sources */,
235251
);
236252
runOnlyForDeploymentPostprocessing = 0;
237253
};

Example/Example/ImageCollectionViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class ImageCollectionViewCell: UICollectionViewCell {
1212
@IBOutlet weak var imageView: UIImageView!
1313
static let identifier = "ImageCollectionViewCell"
1414
static let nib = UINib(nibName: "ImageCollectionViewCell", bundle: nil)
15-
func configure(_ indexPath: IndexPath) {
15+
func configure(indexPath: IndexPath) {
1616
let image = UIImage(named: String(indexPath.item))
1717
imageView.image = image
1818
setNeedsLayout()

Example/Example/MainViewController.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,47 @@ import InfiniteCollectionView
1212
final class MainViewController: UIViewController {
1313
var patterns = ["pattern1", "pattern2"]
1414
let identifier = "tableViewCell"
15+
var cellHeight: CGFloat {
16+
return (UIScreen.main.bounds.height - 64) / CGFloat(patterns.count)
17+
}
1518
@IBOutlet weak var tableView: UITableView! {
1619
didSet {
1720
tableView.delegate = self
1821
tableView.dataSource = self
1922
tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
20-
tableView.rowHeight = 100
21-
tableView.estimatedRowHeight = 100
23+
tableView.rowHeight = cellHeight
24+
tableView.estimatedRowHeight = cellHeight
2225
}
2326
}
2427
override func viewWillAppear(_ animated: Bool) {
2528
super.viewWillAppear(animated)
26-
if let selectedIndexPath = tableView.indexPathForSelectedRow {
27-
tableView.deselectRow(at: selectedIndexPath, animated: true)
28-
}
29+
guard let selectedIndexPath = tableView.indexPathForSelectedRow else { return }
30+
tableView.deselectRow(at: selectedIndexPath, animated: true)
2931
}
3032
}
3133

34+
// MARK: - UITableViewDelegate, UITableViewDataSource
3235
extension MainViewController: UITableViewDelegate, UITableViewDataSource {
3336
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
3437
return patterns.count
3538
}
3639
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
3740
let cell = tableView.dequeueReusableCell(withIdentifier: identifier)!
3841
cell.textLabel?.text = patterns[indexPath.row]
42+
cell.textLabel?.textAlignment = .center
43+
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 24)
3944
return cell
4045
}
4146
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
42-
if indexPath.row == 0 {
47+
switch indexPath.row {
48+
case 0:
4349
let controller = Pattern1ViewController.createFromStoryboard()
4450
navigationController?.pushViewController(controller, animated: true)
45-
} else if indexPath.row == 1 {
51+
case 1:
4652
let controller = Pattern2ViewController.createFromStoryboard()
4753
navigationController?.pushViewController(controller, animated: true)
54+
default:
55+
break
4856
}
4957
}
5058
}

Example/Example/Pattern1.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11535.1" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="jtB-7l-rQl">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="jtB-7l-rQl">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11523"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>

Example/Example/Pattern1ViewController.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import UIKit
1010
import InfiniteCollectionView
1111

1212
final class Pattern1ViewController: UIViewController {
13-
var items = ["1", "2", "3", "4"]
13+
var itemsCount: Int = 5
1414
@IBOutlet weak var collectionView: InfiniteCollectionView! {
1515
didSet {
1616
collectionView.infiniteDataSource = self
1717
collectionView.infiniteDelegate = self
18-
collectionView.cellWidth = UIScreen.main.bounds.width
1918
collectionView.register(ImageCollectionViewCell.nib, forCellWithReuseIdentifier: ImageCollectionViewCell.identifier)
2019
}
2120
}
@@ -26,7 +25,7 @@ final class Pattern1ViewController: UIViewController {
2625
}
2726
@IBOutlet weak var pageControl: UIPageControl! {
2827
didSet {
29-
pageControl.numberOfPages = items.count
28+
pageControl.numberOfPages = itemsCount
3029
}
3130
}
3231
static func createFromStoryboard() -> Pattern1ViewController {
@@ -37,7 +36,6 @@ final class Pattern1ViewController: UIViewController {
3736
super.viewWillTransition(to: size, with: coordinator)
3837
layout.itemSize = size
3938
layout.invalidateLayout()
40-
collectionView.cellWidth = size.width
4139
collectionView.layoutIfNeeded()
4240
collectionView.setNeedsLayout()
4341
}
@@ -46,11 +44,11 @@ final class Pattern1ViewController: UIViewController {
4644
// MARK: - InfiniteCollectionViewDataSource, InfiniteCollectionViewDelegate
4745
extension Pattern1ViewController: InfiniteCollectionViewDataSource, InfiniteCollectionViewDelegate {
4846
func number(ofItems collectionView: UICollectionView) -> Int {
49-
return items.count
47+
return itemsCount
5048
}
5149
func collectionView(_ collectionView: UICollectionView, dequeueForItemAt dequeueIndexPath: IndexPath, cellForItemAt usableIndexPath: IndexPath) -> UICollectionViewCell {
5250
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.identifier, for: dequeueIndexPath) as! ImageCollectionViewCell
53-
cell.configure(usableIndexPath)
51+
cell.configure(indexPath: usableIndexPath)
5452
return cell
5553
}
5654
func infiniteCollectionView(_ collectionView: UICollectionView, didSelectItemAt usableIndexPath: IndexPath) {

0 commit comments

Comments
 (0)