I want that only four specific cells can be selected at one time. When a button is pressed, I want that the selectable cells are 4 indexPath.row
lower.
Example: In the beginning, indexPath.row
44-47 is selectable. If the button is pressed I want, that the indexPath.row
40-43 is selectable and so on.
I thought about making an array with the indexPath and If the button is pressed, the numbers in the array are 4 numbers lower.
Than I don't know, how to add this to the shouldSelectItemAt indexPath
function.
How can I realize this?
You can use an IndexSet.
var allowedSelectionRow: IndexSet
allowedSelectionRow.insert(integersIn: 44...47) //Initial allowed selection rows
In collectionView(_:shouldSelectItemAt:)
return allowedSelectionRow.contains(indexPath.row) //or indexPath.item
Whenever you need:
allowedSelectionRow.remove(integersIn: 44...47) //Remove indices from 44 to 47
allowedSelectionRow.insert(integersIn: 40...43) //Add indices from 40 to 43
Advantage from an Array: Like a set, there is unicity of the values (no duplicates). Contains only integers, and you can add in "range" which can be useful (not add all the indices, but a range).
After comments, if you have only 4 rows allowed and consecutive, you can have that method:
func updateAllowedSectionSet(lowerBound: Int) {
let newRange = lowerBound...(lowerBound+3)
allowedSectionRow.removeAll() //Call remove(integersIn:) in case for instance that you want always the 1 row to be selectable for instance
allowedSectionRow.insert(integersIn: newRange)
}
For the first one, you just need to do:
updateAllowedSectionSet(lowerBound: 44)
instead of allowedSelectionRow.insert(integersIn: 44...47)