Last active
April 11, 2018 15:12
-
-
Save dimitris-c/a82e583eb1609f97dac45c25f9122ce3 to your computer and use it in GitHub Desktop.
Converting an indexPath to the correct index of a one dimension array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let originalArray = [1, 2, 3, 4, 5, 6] | |
let sections = [[1, 2, 3], [5, 6]] | |
let indexPath = IndexPath(section: 0, row: 1) | |
let sectionTotal = self.sections.prefix(upTo: indexPath.section).map { $0.count }.reduce(0, +) | |
let index = sectionTotal + indexPath.row | |
let element = originalArray[index] | |
// element is `2` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment