I wrote an upvote/downvote function for my collectionview cells.
I compare a cell's score variable to the cells above and below it. I have a function containing the following code after the upvote input is received.
if cell != CVcellArray.first && cell != CVcellArray.last {
print("NOW CHECKING IP")
let ip_greater = NSIndexPath(forItem: ip!.item - 1, inSection: ip!.section)
let ip_less = NSIndexPath(forItem: ip!.item + 1, inSection: ip!.section)
let cell_greater = self.collectionView?.cellForItemAtIndexPath(ip_greater) as! CollectionViewCell
let cell_less = self.collectionView?.cellForItemAtIndexPath(ip_less) as! CollectionViewCell
// recursive call to allow the cell to jump over repeat scores
if cell.score > cell_greater.score {
checkIPGreater(ip!, origIp: ip!)
}
else if cell.score < cell_less.score {
checkIPLesser(ip!, origIp: ip!)
}
else {
//do nothing
}
}
else {
print("No need to check ip")
}
checkIPLesser/Greater are functions that look into repeat scores so that a cell might jump multiple spaces if upvoted.
When I upvote/downvote the top/bottom place cells, I crash. This was I assumed, because swift is trying to find a cell greater than the greatest cell, or lower than the lowest cell, and it fails. So I wrote a line that checked if the cell being upvoted was in the first of the cell array, or at the end of the cell array.
Despite this, it still fails. I feel like I'm not properly finding the top or bottom cells.
I have one section, with multiple items within that section (those items being cells). Is there any way I can access the top or bottom place items?