我正在尝试从特定单元格的子视图中触发UITapGesture时从单元格中检索数据。我现在的代码为每个代码返回了错误的单元格,我无法告诉它是如何解决的。有时它会改变同一个细胞。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "followerSeg" || segue.identifier == "followingSeg") {
let vc = segue.destinationViewController as! FollowersFollowingVC
let x = sender as! UITapGestureRecognizer
let view = x.view!
let cellone = view.superview as UIView?
let cell = cellone?.superview as! PersonCell
vc.userName = cell.person.profileName
vc.userId = cell.person.id
vc.segStatus = segue.identifier == "followerSeg" ? 0 : 1
}
}
答案 0 :(得分:1)
您可能想尝试使用UITableView的indexPathForRowAtPoint
方法。我已经和objc / iOS合作了5年多,但这是我在Swift的第一次拍摄。请原谅任何语法错误。
假设您有一个名为tableView
的属性,代码如下:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "followerSeg" || segue.identifier == "followingSeg") {
let vc = segue.destinationViewController as! FollowersFollowingVC
let x = sender as! UITapGestureRecognizer
let location = x.locationInView(self.tableView) as CGPoint
let indexPath = self.tableView.indexPathForRowAtPoint(location) as NSIndexPath?
let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! PersonCell
vc.userName = cell.person.profileName
vc.userId = cell.person.id
vc.segStatus = segue.identifier == "followerSeg" ? 0 : 1
}
}