首页 文章

collectionViewCells互动

提问于
浏览
1

我有一个集合视图,每个单元格都有一个按钮,我添加了一个按下按钮时调用的IB动作 .

我的问题是,当点击某个按钮时,我不仅要更改该按钮的背景颜色,还要更改所有单元格中的所有按钮

我不确定如何实现这个......

谢谢 .

2 回答

  • 0

    试试这样的Somthg

    拿一个 var selectedIndex : Int = -1

    cellForItemAt

    cell.button.tag = indexpath.item
    if selectedIndex == indexPath.item{
       cell.button.backgroundColor = UIColor.blue // New Color
    }
    else{
       cell.button.backgroundColor = UIColor.gray // Deafult color
    }
    

    Button Action

    selectedIndex = sender.tag
    collectionView.ReloadData()
    
  • 1

    当您按下按钮时,您应该通知您的collectionView类持有者(通过 delegate 模式或使用 closure ),现在应该更改所有按钮 . 然后你应该强制改变所有可见的按钮(采取所有 visibleCells 的collectionView并更改按钮样式),并且在 cellForItemAtIndexPath 方法中更改每个其他按钮的按钮样式,这些按钮现在是不可见的 .

相关问题