user190494
user190494

Reputation: 509

How to remove or dim a cell at location in UICollectionView

I am trying to remove a or dim a cell from the collectionview at a specific index location, but I cannot seem to find documentation for this. How would I approach this? I am trying to perform this in the updateScore method. Thanks.

class ViewController2: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var tableImages: [String] = ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png", "image7.png", "image8.png", "image9.png", "image10.png", "image11.png", "image12.png", "image13.png", "image14.png", "image15.png", "image16.png", "image17.png", "image18.png", "image19.png", "image20.png", "image21.png", "image22.png", "image23.png", "image24.png", "image25.png", "image26.png", "image27.png", "image28.png", "image29.png", "image30.png"]

var location:Int = 0


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return tableImages.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell


    cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
    return cell

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Cell \(indexPath.row) selected")

    location = indexPath.row

    self.performSegueWithIdentifier("popUpSegue", sender: nil)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "popUpSegue") {
        let svc = segue.destinationViewController as! ViewController3
        svc.location = location

    }
}

static func updateScore(location: Int){
    print(location)

    // remove cell at location

}

static func updateScore2(location: Int){
    print(location)

    // dim cell at location

}

Upvotes: 0

Views: 469

Answers (2)

Victor Sigler
Victor Sigler

Reputation: 23449

You can remove a cell for an index using the tableView:deleteItemsAtIndexPaths but you need to take in account to keep updated your dataSource like in the following way:

static func updateScore(location: Int){

    // create the indexPath
    var indexPath = NSIndexPath(forRow: location, inSection: 0)

    self.tableView.beginUpdates()

    // delete the row at indexPath , you can hanlde the animation you want too
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .None)

    // update your dataSource size
    self.tableImages.removeAtIndex(location)

    self.tableView.endUpdates()
}

Every time you add/remove a table item the tableView:numberOfRowsInSection: method is called - unless you surround these calls with begin/endUpdate. If your array and table view items are out of sync, without the begin/end calls an exception will be thrown.

I hope this help you.

Upvotes: 1

zrzka
zrzka

Reputation: 21239

First, you have to remove it from your tableImages array:

tableImages.removeAtIndex(location)

Then you have to delete it from collection view:

let indexPath = NSIndexPath(forRow: location, inSection: 0)
collectionView.deleteItemsAtIndexPaths([indexPath])

Keep in mind - your backing store (tableImages) and collection view must be kept in sync.

Upvotes: 0

Related Questions