滚动

时间:2016-02-19 07:44:44

标签: ios objective-c swift uicollectionview swift2

您好我在我的应用中使用了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

这是集合视图加载时的图像,我选择一个单元格。但是当我滚动所有选定的单元格时,背景将变为默认值。那怎么能解决这个问题呢。我想要的是如果我在滚动集合视图后选择任何单元格,则必须选择单元格背景

2 个答案:

答案 0 :(得分: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
                }

答案 1 :(得分:-1)

在您的collectionView数据源方法中使用它... 当这里的集合视图流口水是解决方案时你会遇到这个问题试试这个!!

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

}