gpt4 book ai didi

ios - 触发 UITapGesture 时从单元格的 subview 中获取单元格的引用

转载 作者:可可西里 更新时间:2023-11-01 02:19:29 24 4
gpt4 key购买 nike

当从特定单元格的 subview 中触发 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
}
}

最佳答案

您可能想尝试使用 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
}
}

关于ios - 触发 UITapGesture 时从单元格的 subview 中获取单元格的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com