2016-02-19 183 views
0

嗨,我在我的應用程序中使用collectionview。當我選擇任何單元格時,它的工作狀態很好,我只是改變了單元格的背景圖片。但是當我滾動收集視圖,更改背景圖像自動更改爲默認圖像。這是我的問題。滾動時收藏查看更改單元格背景圖像

這裏是我的代碼

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 
} 

enter image description here

這是當集合視圖負荷,我選擇一個單元區域的圖像。但是當我滾動所有我選擇的單元格背景默認。所以我該如何解決這個問題。我想要的是細胞背景必須選擇,如果我選擇任何細胞,即使滾動收集後視圖

+0

存儲在一些陣列選擇單元的指數和比檢查在'cellForItemAtIndexPath'該數組包含相應當前索引與否,並設置圖像。 –

+0

抱歉,您能否給我一行代碼? @EICaptain。 –

回答

1

您需要維護每個座位的狀態,如選擇或不選擇。基於狀態需要設置圖像中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 
       } 
+0

已經嘗試過。但是這會導致所有的座位被選中時滾動 –

+0

你怎麼保持狀態? – Savitha

+0

你可以在SeatDetailModel中維護狀態 – Savitha

-1
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"]; 

}