0

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?

user3488148
  • 59
  • 1
  • 8

1 Answers1

1

I don't see the cause of the crash, but since you're force-unwrapping ip and you haven't showed us where ip comes from, I'm suspicious about that. If you need help nailing down the specific cause, it'd help if you posted more code and the stack trace from a crash.

Nevertheless, I think you're going about this the wrong way. Instead of dealing with cells and index paths, you should be dealing with the underlying data. Your collection view has a data source object that's responsible for populating the cells, and that data source probably interacts with some kind of data model. The numbers of up and down votes are attributes of the items in the data model, and any changes should be dealt with by the model.

If you implement things that way, then all your collection view controller needs to do is recognize that an up or down vote has occurred and pass that information on to the model. The model can then update the data and rearrange the items, which could be as simple as sorting the array. The model would then notify any listeners that the items have changed, and the collection view controller would reload the cells.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Okay sure, but I'm not sure how to perform the "The model can then update the data and rearrange the items," part. I would have to reload the entire collection view right? – user3488148 Jan 20 '16 at 18:35