【问题标题】:Button Tap Action in UICollectionViewCell in UITableViewCellUITableViewCell 中的 UICollectionViewCell 中的按钮点击操作
【发布时间】:2019-01-23 11:24:02
【问题描述】:

我有这个层次结构。

UITableView -> UITableViewCell -> UICollectionView -> UICollectionViewCell -> UIButton。 如何使用 swift4 处理 uibutton 上的点击事件?

【问题讨论】:

  • 你想在哪里处理按钮的动作?在 ViewController 或其他地方?

标签: swift uitableview uicollectionview uibutton uicollectionviewcell


【解决方案1】:

请试试这个:

tableViewcellForRowAtIndexPath方法中:

cell.yourCollectionView.tag = indexPath.row

CollectionViewcellForRowAtIndexPath方法中:

cell.yourBtn.tag = collectionView.tag * 2000 + indexPath.item

在按钮操作中请写:

let collectionViewTag = sender.tag / 2000
let index = sender.tag % 2000

它可能对您有所帮助。谢谢。

【讨论】:

    【解决方案2】:

    cellForRowAt里面做

    let cell = ///
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self  
    

    之后,您就可以在 vc 中实现 cellForItemAt ,然后执行

    let collecCell = ///
    collecCell.button.addTarget//////
    

    @objc func buttonClicked(_ bt:UIButton) {}
    

    关于知道哪个项目可以使用tag 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多