【问题标题】: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)
}