【问题标题】:PrepareForReuse in CollectionViewCellCollectionViewCell 中的 PrepareForReuse
【发布时间】:2016-03-02 13:02:09
【问题描述】:

我想将标签隐藏在被点击的单元格中,而是显示图像。但我只想在具有特定索引的单元格已设置为 imageView 时才这样做。 如果将单元格设置为 imageView,则处理单元格和存储的最佳方法是什么?如何使用prepareForReuse 方法?

直到现在我都是这样做的,但是因为单元格被重复使用。图片在滚动时显示在其他单元格中。

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    println("user tapped on door number \(indexPath.row)")

    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

    if (cell.myLabel.text == "1") {
        one = true

        if(seven = true) {

            if (cell.myLabel.hidden) {
                cell.myLabel.hidden = false
                cell.MyImageView.image = nil

            }
            else {
                cell.myLabel.hidden = true
                cell.MyImageView.image = UIImage(named:"1")!
            }

         }
    }

【问题讨论】:

    标签: ios swift uicollectionviewcell collectionview prepareforreuse


    【解决方案1】:

    在我的应用程序中,我必须根据索引路径配置 UICollectionReusableView,如果 indexPath 具有特定值,那么我发送一个用于设置标签和图像的数组。

    我在自定义 UICollectionReusableView 中使用了一个函数,如果我用数组调用它,它会填充标签和图像,如果我用 nil 调用它,它会重置这些。

    func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView!  {
         .... [logic around selecting index path based on data returned]
         .... 
       if filteredEvents != nil{
                reusableView.populateCalendarDayDates(sortedEvents)
       }else{
                reusableView.populateCalendarDayDates(nil)
       }
    

    在自定义 UICollectionReusableView 的函数中,我在可能更新标签之前将标签重置为默认值:

    func populateCalendarDayDates(arrayEvents: NSArray?){
        let firstDayTag = tagStartDay()
        var dayDate = 1
    
        for var y = 1; y < 43; y++ {
            let label = self.viewWithTag(y) as! BGSCalendarMonthLabel
            label.delegate = callingCVC
            label.backgroundColor = UIColor.clearColor()
            label.textColor = UIColor.whiteColor()
    
            label.text = ""
    

    通过将这段代码移动到自定义 UICollectionReusableView 中的 prepareForReuse ,您可以获得相同的效果,并且它可能更具可读性:

    override func prepareForReuse() {
        super.prepareForReuse()
        for var y = 1; y < 43; y++ {
            let label = self.viewWithTag(y) as! BGSCalendarMonthLabel
            label.backgroundColor = UIColor.clearColor()
            label.textColor = UIColor.whiteColor()
    
            label.text = ""
        }
    
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您没有说您的集合视图是否正好有 7 个单元格,或者集合中是否可以有“N”个(例如 100 个)单元格,所以如果这是我的问题并且我必须解决它,我会“seven”单元格的状态是类的属性(例如“var sevenState : Bool”),然后我可以根据sevenState 的内容显示其他单元格的按钮或图像。

      【讨论】:

      • 我有确切数量的单元格,但每个单元格都“连接”到另一个单元格,因此您可以按预定义的顺序点击它们
      • 你到底有多少个单元格?如果您需要保留某些单元格的状态信息,那么您需要有一些底层数据结构(无论是 Bool 属性、数组还是其他东西)来跟踪各个单元格的状态。跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多