滚动时,collectionview更改单元格背景图像

嗨,我在我的应用程序中使用collectionview。 和它的工作,当我select任何细胞,我只是改变该单元格的背景图像。 但是当我滚动集合视图,更改背景图像自动更改为默认图像。 这是我的问题。

这是我的代码

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 } 

在这里输入图像说明

这是收集视图加载和我select一个单元格的图像。 但是当我滚动所有我select的单元格背景默认。 所以我该如何解决这个问题。 我想要的是单元格背景必须select,如果我select任何细胞,甚至滚动collections视图后

您需要像select或不select一样保持每个座位的状态。 基于状态你需要在cellForItemAtIndexPath中设置图像

 if (data.DurumYan == "2") && (data.Durum == "0") { var image:UIImage? if selected == true { //image = selected image } else { // image = non selected image } cell.backgroundView = UIImageView(image: image!) cell.userInteractionEnabled = true } 
 Use this in Your collectionView Datasource Method ... you are getting this problem when collection view scrooling here is solution try this !! - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.background.image=nil; cell.background.image=[UIImage imageNamed:@"background.png"]; }