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

戈文德·拉科里亚(Govind Rakholiya)

嗨,我在我的应用程序中使用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
}

在此处输入图片说明

这是收集视图加载时的图像,我选择一个单元格。但是,当我滚动时,所有选中的单元格背景都将变为默认背景。所以我该怎么解决呢。如果我选择了任何单元格,即使在滚动集合视图后也必须选择单元格背景

萨维莎

您需要保持每个座位的状态,无论是否选中。基于状态,您需要在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
                }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

每个单元格的背景图像

来自分类Dev

滚动时背景图像UITableView更改

来自分类Dev

collectionView单元格滚动时图像变化-快速-以编程方式

来自分类Dev

通过表格单元格显示背景图像?

来自分类Dev

滚动时单元格元素放错位置-CollectionView

来自分类Dev

滚动时的uitableview单元格背景色问题

来自分类Dev

UICollectionView带图像的单元格,单击更改背景

来自分类Dev

滚动时自定义单元格图像变形

来自分类Dev

滚动时自定义单元格图像变形

来自分类Dev

在表格视图中滚动时图像重复单元格

来自分类Dev

UITableView单元格图像在滚动时保持切换单元格的状态

来自分类Dev

TableView在滚动时更改单元格内容

来自分类Dev

CollectionView,更改单元格颜色

来自分类Dev

更改CollectionView单元格的垂直间距

来自分类Dev

如何快速设置表格单元格的背景图像大小?

来自分类Dev

如何将背景图像与CSS中表格单元格的底部对齐?

来自分类Dev

带有透明单元格并显示背景图像的快速表格视图

来自分类Dev

为什么这个div单元格内的背景图像完全不显示?

来自分类Dev

如何在ios7的uitableview单元格中设置背景图像?

来自分类Dev

将背景图像添加到单个表格单元格

来自分类Dev

关闭压缩单元格背景图像-Objective-C(Xcode)

来自分类Dev

表格单元格中的CSS无边界背景图像

来自分类Dev

如何在xamarin.forms中将背景图像设置为输入单元格

来自分类Dev

滚动时如何放大背景图像?

来自分类Dev

Xamarin CollectionView滚动时突出显示不正确的单元格

来自分类Dev

按下按钮时增加 collectionview 单元格的高度并滚动到底部

来自分类Dev

Swift 3如何在CollectionView中选择单元格时设置不同的背景色

来自分类Dev

背景图像滚动

来自分类Dev

SWIFT中CollectionView内部的单元格自动滚动

Related 相关文章

  1. 1

    每个单元格的背景图像

  2. 2

    滚动时背景图像UITableView更改

  3. 3

    collectionView单元格滚动时图像变化-快速-以编程方式

  4. 4

    通过表格单元格显示背景图像?

  5. 5

    滚动时单元格元素放错位置-CollectionView

  6. 6

    滚动时的uitableview单元格背景色问题

  7. 7

    UICollectionView带图像的单元格,单击更改背景

  8. 8

    滚动时自定义单元格图像变形

  9. 9

    滚动时自定义单元格图像变形

  10. 10

    在表格视图中滚动时图像重复单元格

  11. 11

    UITableView单元格图像在滚动时保持切换单元格的状态

  12. 12

    TableView在滚动时更改单元格内容

  13. 13

    CollectionView,更改单元格颜色

  14. 14

    更改CollectionView单元格的垂直间距

  15. 15

    如何快速设置表格单元格的背景图像大小?

  16. 16

    如何将背景图像与CSS中表格单元格的底部对齐?

  17. 17

    带有透明单元格并显示背景图像的快速表格视图

  18. 18

    为什么这个div单元格内的背景图像完全不显示?

  19. 19

    如何在ios7的uitableview单元格中设置背景图像?

  20. 20

    将背景图像添加到单个表格单元格

  21. 21

    关闭压缩单元格背景图像-Objective-C(Xcode)

  22. 22

    表格单元格中的CSS无边界背景图像

  23. 23

    如何在xamarin.forms中将背景图像设置为输入单元格

  24. 24

    滚动时如何放大背景图像?

  25. 25

    Xamarin CollectionView滚动时突出显示不正确的单元格

  26. 26

    按下按钮时增加 collectionview 单元格的高度并滚动到底部

  27. 27

    Swift 3如何在CollectionView中选择单元格时设置不同的背景色

  28. 28

    背景图像滚动

  29. 29

    SWIFT中CollectionView内部的单元格自动滚动

热门标签

归档