Krutarth Patel
Krutarth Patel

Reputation: 3455

in Collectionview how to set colors according to selection?

My requirement is to set by default color at first indexPath and I want to change colors according to selection.

My Requirement is when collectionview will load at first index background color should be Red Color.and deselected color is blue color. how to manage this Here is my code

import UIKit
class MyCollectionViewCell: UICollectionViewCell {
override var selected: Bool {
    didSet {
        contentView.backgroundColor = selected ? UIColor.redColor(): UIColor.blueColor()
    }
}
}

class ViewController: UIViewController {
@IBOutlet var collView: UICollectionView!

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

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

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCollectionViewCell", forIndexPath: indexPath) as! MyCollectionViewCell
    if(indexPath.row == 0) { //for first cell in the collection
        cell.backgroundColor = UIColor.redColor()
    } else {
        cell.backgroundColor = UIColor.blueColor()
    }

    return cell
}

}

by this code my output is

Issue

Issue is when I click on first indexPath it works but by selecting others my two colors are same

Upvotes: 0

Views: 1104

Answers (2)

pedrouan
pedrouan

Reputation: 12910

If you meant that you have preselect your first cell, then follow NDOC's answer.

Just a better explanation of your methods:

In cellForItemAt method:

if(indexPath.row == 0) { //for first cell in the collection
    cell.backgroundColor = UIColor.redColor() //this is the color of the first row
} else {
    cell.backgroundColor = UIColor.blueColor() //this is the color of the other rows
}

In MyCollectionViewCell Subclass:

//if any cell (first or other) is selected, it's bg color will be red
//if any cell will be deselected, it's bg color will be blue
didSet {
        contentView.backgroundColor = selected ? UIColor.redColor(): UIColor.blueColor()
    }

It may confuse you, so change this line code to

contentView.backgroundColor = selected ? UIColor.yellowColor(): UIColor.greenColor()

and you'll figure out, how it works.

Upvotes: 1

Nirav D
Nirav D

Reputation: 72450

In your viewDidLoad reload the collectionView and after that explicitly select the first cell like this way.

collView.reloadData()
let indexPath = NSIndexPath(forItem: 0, inSection: 0)
collView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: .None)

Note : Now there is no need to add code of color changing inside your cellForIteamAtIndexPath remove that code.

Upvotes: 1

Related Questions