Определите следующие переменные:
var firstSelectedIndex: Int?
var nextEligibleCount: Int = 0
Затем обновите метод делегата collectionView:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch indexPath.row {
case 0, 4, 8, 12:
firstSelectedIndex = indexPath.row
print("-----New row click has been started------")
print(String(indexPath.row) + " is clicked")
nextEligibleCount = 1
default:
checkClick(atIndex: indexPath.row)
}
}
checkClick () определяется какниже:
func checkClick(atIndex selectedIndex: Int) {
if firstSelectedIndex != nil {
if selectedIndex == firstSelectedIndex! + nextEligibleCount {
print(String(selectedIndex) + " is clicked")
nextEligibleCount = nextEligibleCount + 1
} else {
print("Invalid click")
}
}
}