uicollectionview细胞背景图像重复出现

时间:2016-02-18 12:21:55

标签: ios swift uicollectionview

在我的应用程序中我正在使用collectionview.so我在模型中使用模型和插入响应数组并在特定单元格中设置特定图像。当集合视图加载时它显示完美但当我滚动图像获取更改图像正在加载在我不想显示它并继续更改的地方。

这是我的代码

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : seat_cell = collectionView.dequeueReusableCellWithReuseIdentifier("seat_cell", forIndexPath: indexPath) as! seat_cell
    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 = ""
    }

    else
    {
        cell.lblname.text = data.KoltukStr


        if (data.DurumYan == "2") && (data.Durum == "0")
        {
            //cell.img_seat.image = UIImage(named: "seat_men")
            //cell.backgroundColor = UIColor(patternImage: UIImage(named: "seat_men")!)
            cell.backgroundView = UIImageView(image: UIImage(named: "seat_men"))

        }
        else if (data.DurumYan == "1") && (data.Durum == "0")
        {


            cell.backgroundView = UIImageView(image: UIImage(named: "seat_lady"))
        }
        else if (data.Durum == "0")
        {

            cell.backgroundView = UIImageView(image: UIImage(named: "seat_green"))
        }
        else
        {

            cell.backgroundView = UIImageView(image: UIImage(named: "blank_seat"))
        }
    }
    return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell : seat_cell = collectionView.dequeueReusableCellWithReuseIdentifier("seat_cell", forIndexPath: 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
    {

         cell.backgroundView = UIImageView(image: UIImage(named: "seat_lady"))

    }
   [![This is the screen which load when collectionview load this is perfect][1]][1]

}

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

    return get_seat_detail.count
}

this is the view which i got while i scroll uicollectionview

在图像中,您可以显示我在滚动时获得的视图。但是当最初的集合视图加载它的显示完美意味着它没有显示没有数字的座位是完美的

1 个答案:

答案 0 :(得分:0)

您的单元格正在重复使用,当您创建新单元格时,始终清除单元格项目的值,在下面的方法中

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : seat_cell = collectionView.dequeueReusableCellWithReuseIdentifier("seat_cell", forIndexPath: indexPath) as! seat_cell // After this line
    cell.backgroundView = UIImageView() // clear old references

为默认的imageview添加此行,这将清除旧的引用。