-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexPath+Extension.swift
65 lines (56 loc) · 2.08 KB
/
IndexPath+Extension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// IndexPath+Extension.swift
// Extensions
//
// Created by Mohd Tahir on 29/07/19.
// Copyright © 2019 Mohd Tahir. All rights reserved.
//
import UIKit
extension IndexPath {
public func next(in tableView: UITableView) -> IndexPath? {
var newIndexPath = IndexPath(row: row + 1, section: section)
if newIndexPath.row >= tableView.numberOfRows(inSection: section) {
let newSection = section + 1
newIndexPath = IndexPath(row: 0, section: newSection)
if newSection >= tableView.numberOfSections {
return nil
}
}
return newIndexPath
}
public func previous(in tableView: UITableView) -> IndexPath? {
var newIndexPath = IndexPath(row: row - 1, section: section)
if newIndexPath.row < 0 {
let newSection = section - 1
if newSection < 0 {
return nil
}
let maxRow = tableView.numberOfRows(inSection: newSection) - 1
newIndexPath = IndexPath(row: maxRow, section: newSection)
}
return newIndexPath
}
public func next(in collectionView: UICollectionView) -> IndexPath? {
var newIndexPath = IndexPath(row: row + 1, section: section)
if newIndexPath.row >= collectionView.numberOfItems(inSection: section) {
let newSection = section + 1
newIndexPath = IndexPath(item: 0, section: newSection)
if newSection >= collectionView.numberOfSections {
return nil
}
}
return newIndexPath
}
public func previous(in collectionView: UICollectionView) -> IndexPath? {
var newIndexPath = IndexPath(row: row - 1, section: section)
if newIndexPath.row < 0 {
let newSection = section - 1
if newSection < 0 {
return nil
}
let maxRow = collectionView.numberOfItems(inSection: newSection) - 1
newIndexPath = IndexPath(item: maxRow, section: newSection)
}
return newIndexPath
}
}