【问题标题】:Adding button to UICollectionView cells将按钮添加到 UICollectionView 单元格
【发布时间】:2018-08-03 01:56:35
【问题描述】:

如何在 swift 4 中向每个水平集合视图单元格添加一个按钮并将其链接到一个动作

【问题讨论】:

    标签: xcode uicollectionview uibutton swift4 uicollectionviewcell


    【解决方案1】:

    喜欢这个连接按钮 IBAction 在你的 CollectionViewCell 类中。

    class PhotosCollectionViewCell: UICollectionViewCell {
    
       @IBAction func DeleteTapped(_ sender: Any) {
    
           if let OnDeleteTapped = self.OnDeleteTapped {
                OnDeleteTapped(self)
           }
       }
    
       var OnDeleteTapped : ((PhotosCollectionViewCell) -> Void)? = nil   
    }
    

    在 CollectionView Delegate 方法中使用它。

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell
    
        cell.OnDeleteTapped = { cell in
              // Do Anything Here is Your Action Event
        }
    
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-10
      • 2014-07-11
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多