hi i am using collectionview in my app. and its working well when i select any cell i just change background image of that cell. but when i scroll collection view that changed background image automatically change to the default image. thats my issue.
here is my code
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : seat_cell = collectionView.dequeueReusableCellWithReuseIdentifier("seat_cell", forIndexPath: indexPath) as! seat_cell
cell.backgroundView = UIImageView()
let data : SeatDetailModel = get_seat_detail.objectAtIndex(indexPath.row) as! SeatDetailModel
if (data.KoltukStr == "PI") || (data.KoltukStr == "MA") || (data.KoltukStr == "SA") || (data.KoltukStr == "KA") || (data.KoltukStr == "KO")
{
cell.backgroundColor = UIColor.clearColor()
cell.lblname.text = ""
cell.backgroundView = nil
}
else
{
cell.lblname.text = data.KoltukStr
if (data.DurumYan == "2") && (data.Durum == "0")
{
cell.backgroundView = UIImageView(image: UIImage(named: "seat_men"))
cell.userInteractionEnabled = true
}
else if (data.DurumYan == "1") && (data.Durum == "0")
{
cell.backgroundView = UIImageView(image: UIImage(named: "seat_lady"))
cell.userInteractionEnabled = true
}
else if (data.Durum == "0")
{
//cell.backgroundColor = UIColor(patternImage: UIImage(named: "seat_green")!)
cell.backgroundView = UIImageView(image: UIImage(named: "seat_green"))
cell.userInteractionEnabled = true
}
else
{
if (data.Durum == "1") || (data.Durum == "3") || (data.Durum == "5")
{
cell.backgroundView = UIImageView(image: UIImage(named: "blank_seat"))
cell.userInteractionEnabled = false
}
else
{
cell.backgroundView = UIImageView(image: UIImage(named: "blank_seat"))
}
}
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell : seat_cell = collectionView.cellForItemAtIndexPath(indexPath) as! seat_cell
let data : SeatDetailModel = get_seat_detail.objectAtIndex(indexPath.row) as! SeatDetailModel
if (Gender.isEmpty)
{
alertViewShow(self, title: "Alert", message: "Please Select Gender before Seat Selection")
}
else
{
seatcount = seatcount + 1
if (data.DurumYan == "1") && (data.Durum == "0")
{
cell.backgroundView = UIImageView(image: UIImage(named: "seat_lady_checked"))
selectedseat = true
}
else if (data.DurumYan == "2") && (data.Durum == "0")
{
cell.backgroundView = UIImageView(image: UIImage(named: "seat_men_checked"))
selectedseat = true
}
else if (data.Durum == "0")
{
cell.backgroundView = UIImageView(image: UIImage(named: "seat_green_checked"))
selectedseat = true
}
}
print(data.KoltukStr)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return get_seat_detail.count
}
this is the image of when collection view load and i select one cell. but when i scroll all my selected cell background comes to default. so how can i solve that. what i want is cell background must be selected if i select any cell even after scrolling collection view
You need to maintain state of each seat like selected or not. based on state you need to set image in cellForItemAtIndexPath
Use this in Your collectionView Datasource Method ... you are getting this problem when collection view scrooling here is solution try this !!