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

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

本文介绍了滚动时CollectionView更改单元格背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在我的应用程序中使用collectionview.当我选择任何单元格时,它的效果很好,我只是更改了该单元格的背景图像.但是,当我滚动收藏夹视图时,更改的背景图像会自动更改为默认图像.多数民众赞成在我的问题.

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.

这是我的代码

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

推荐答案

您需要保持每个席位的状态,无论是否选中.根据状态,您需要在cellForItemAtIndexPath中设置图像

You need to maintain state of each seat like selected or not. based on state you need to set image in 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
                }

这篇关于滚动时CollectionView更改单元格背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:27