【问题标题】:Changing a single UIButton image in a collection view is affecting multiple cells更改集合视图中的单个 UIButton 图像会影响多个单元格
【发布时间】:2015-07-07 14:20:25
【问题描述】:

我目前正在使用集合视图向用户显示事件列表,并且我的每个自定义单元格都有一个按钮,邀请用户参加事件。按下时,按钮图像应更改为 newImage.png,显示他们现在正在参加该活动。当我在下面的代码中执行此操作时,按下按钮实际上会更改图片,但是当我向下滚动收藏视图时,尚未单击的多个单元格也已更改为“newImage.png”。 我怎样才能阻止这种情况发生?

class CustomCell: UICollectionViewCell{

 @IBAction func myButtonAction(sender: UIButton) {
        myButtonOutlet.setImage(UIImage(named: "newImage.png"), forState: UIControlState.Normal)
    }

 @IBOutlet weak var myButtonOutlet: UIButton!
}

【问题讨论】:

    标签: ios swift uibutton uicollectionview uicollectionviewcell


    【解决方案1】:

    collection view 正在重用单元格,正如它设计的那样。你应该做的是在你的 cellForItemAtIndexPath 实现中重置图像。

    【讨论】:

    • 这是否涉及将按钮的出口移动到我的 CollectionViewClass 中?然后我如何传达在特定单元格上按下了按钮?
    • 为什么你的单元格内需要一个按钮?你不能只实现 collectionView:didSelectItemAtIndexPath 吗?
    • 目前我的 didSelectItemAtIndexPath 执行一个更详细地查看事件的序列。该按钮应该是一种无需移动到其他视图控制器即可快速参加活动的方式。
    • 明白了。为什么不创建一个带有嵌入式按钮的自定义单元格,并让每个单元格在其按钮被点击时回调到您的类?这样,单元格负责管理自己的按钮等。
    • 嵌入式按钮到底是什么意思?
    【解决方案2】:

    我以前也遇到过这个问题,这很可能是单元重用。为了避免这个问题,您可能会尝试做的是在您的cellForItemAtIndexPath() 中显式设置单元格的图像,然后在您的模型中添加一些东西来跟踪用户正在参加的活动。然后,再次在您的cellForItemAtIndexPath() 中,检查模型以查看该单元格上应该有什么按钮,然后相应地进行更改。

    【讨论】:

      【解决方案3】:

      您需要将选定的按钮索引存储在您的类中并检查您的函数中的特定索引

        override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
      cell.backgroundColor = UIColor.blackColor()
      if(selectedIndex == indexPath.row){
           myButtonOutlet.setImage(UIImage(named: "newImage.png"), forState: UIControlState.Normal)
      
        //change background image here also your button code
      }
      return cell
      }
      

      在完成这些步骤之后。重新加载集合视图。

      【讨论】:

        【解决方案4】:

        这最终解决了我的问题。我有一个存储单元格的数组。每个单元格都有一个名为isAttending 的布尔值。在我的cellForItemAtIndexPath 方法中,我实现了下面的代码以及函数switchImage

         func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CalendarCell", forIndexPath: indexPath) as! CalendarCell
        
            cell.customButton.layer.setValue(indexPath.row, forKey: "index")
            cell.customButton.addTarget(self, action: "switchImage:", forControlEvents: UIControlEvents.TouchUpInside)
        
            return cell
        }
        
        
        
          func switchImage(sender: UIButton){
            let index : Int = (sender.layer.valueForKey("index")) as! Int
            if (events[index].isAttending == false){
              events[index].isAttending = true
              cell.customButton.setImage(UIImage(named: "isAttending.png"), forState: UIControlState.Normal)
           }else{
              events[index].isAttending = false
              cell.customButton.setImage(UIImage(named: "isNotAttending.png"), forState: UIControlState.Normal)
                }
            self.collectionView.reloadData()
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-06
          • 2019-02-05
          • 1970-01-01
          • 2016-05-26
          • 2020-07-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多