In CollectionView, I want to get all cells' indexPath.row around the pressed cell .
I had tried to get the pressed cell's position, and then use
collectionView.indexPathForItem(at: CGPoint)
let cellsPerRow = 3
let t = pressedCell.index - 3
if t >= 0{
top = t
}
let b = pressedCell + 3
if b <= 9{
bottom = b
}
pressedCell.index%cellsPerRow
var indexArroundDict = [String:Int]()
let top = index - cellsPerRow
if top >= 0 {
indexArroundDict["top"] = index - cellsPerRow
}
let b = index + cellsPerRow
if b < totalCellArr.count {
indexArroundDict["bottom"] = b
}
if index%cellsPerRow != 0 {
indexArroundDict["left"] = index - 1
}
if index%cellsPerRow != cellsPerRow-1 {
indexArroundDict["right"] = index + 1
}
This should do the trick:
let cellsPerRow = 3
let numberOfRows = 3
let index = pressedCell.index
if index > cellsPerRow {
top = index - cellsPerRow
}
if index < (numberOfRows - cellsPerRow) {
bottom = b
}
if index%cellsPerRow != 1 {
left = index -1
}
if index%cellsPerRow != 0 {
right = index + 1
}