【问题标题】:collectionview change cell background image when scrolling滚动时collectionview更改单元格背景图像
【发布时间】:2016-02-19 07:44:43
【问题描述】:

嗨,我在我的应用程序中使用 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 中该数组是否包含当前索引并相应地设置图像。
  • 对不起,你能给我一行代码吗? @EICaptain。

标签: ios objective-c swift uicollectionview swift2


【解决方案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
                }

【讨论】:

  • 已经试过了。但这会导致滚动时选择所有座位
  • 你是如何维护状态的?
  • 可以在 SeatDetailModel 中维护状态
  • 非常感谢@Savitha,我只是维护模型中的状态及其对我的工作。会为你应得的答案投票
【解决方案2】:

在您的 collectionView 数据源方法中使用它... 当collection view scrooling here是解决方案时,你会遇到这个问题试试这个!!

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

}

【讨论】: