2016-02-19 123 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"]; 

}