【问题标题】:How to the get the position of a UISwitch inside a UICollectionViewCell in Swift如何在 Swift 中的 UICollectionViewCell 中获取 UISwitch 的位置
【发布时间】:2016-07-14 06:35:36
【问题描述】:

我有一个UICollectionView,一个UIImageView 和一个UISwitch。单击UISwitch 时,我想检索Cell 的索引。

我可以通过单击单元格中的UIImageView 来检索单元格的索引,但不能使用didSelectItemAtIndexPath 进行切换。

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell uiswitch


    【解决方案1】:

    切换UISwitch时不会选择单元格,所以你必须使用其他方式来识别项目。

    通常由以下人员完成:

    • 为交换机使用专用目标
    • 或者使用标签来识别开关

    【讨论】:

      【解决方案2】:

      这将是您想要的解决方案,我使用tableView 方法获取indexPath,具体取决于tableView 中的按钮位置

      func switchTap(sender: UISwitch) {
           //Get the point in cell
          let center: CGPoint = sender.center;
          let rootViewPoint: CGPoint = sender.superview.convertPoint(center, toView: tableView)
           // Now get the indexPath
          let indexPath: NSIndexPath = tableView.indexPathForRowAtPoint(rootViewPoint)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 2014-10-24
        • 2018-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-03
        相关资源
        最近更新 更多