【发布时间】:2017-06-03 13:30:26
【问题描述】:
首先我是 swift 的新手,所以要温柔。我在获取单元格外部单元格的indexPath 时遇到问题。我想获取函数的索引路径并使用它将内容发送到另一个视图。图像是在单元格中以编程方式创建的,它有一个轻击手势识别器发送到此函数(其中“myIndex”被定义为Int : var myIndex = 0):
// tap image to go at the indexpath of the user
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
print("image tapped")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "patientProfile") as! PatientProfileVC
secondViewController.userID = posts[myIndex].userID
secondViewController.patientName = posts[myIndex].author
print("Passed data to the other view")
self.show(secondViewController, sender: self)
}
我知道我可以在 IndexPath 中使用 didSelectItem,在那里我也有 myIndex 的声明,但我有一些东西会从那里的 segue 触发:
//standard functon for item selected
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//get the index of the cell
myIndex = indexPath.row
print("Cell is selected")
//perform the segue
self.performSegue(withIdentifier: "CommentsVC", sender: self)
print(myIndex)
}
我的 segue 表演看起来像这样:` override
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("Segue Initiated")
feed.navigationItem.title = nil
//if segue.destination is CommentsFullVC{
print("Comments segue initiated")
let secondViewController = segue.destination as! CommentsFullVC
secondViewController.questionData = posts[myIndex].postContent
secondViewController.authorName = posts[myIndex].author
secondViewController.date = posts[myIndex].normalDate
secondViewController.profileImagePath = posts[myIndex].pathToImage
secondViewController.typeOfClinic = posts[myIndex].clinic
secondViewController.postID = posts[myIndex].postID
secondViewController.followersForPost = posts[myIndex].followers
}
所以我的问题是,如何在不使用 prepareForSegue 的情况下将这两个字段传递给另一个视图?我现在为此苦苦挣扎了 20 个小时,但仍然无法弄清楚。
secondViewController.userID = posts[myIndex].userID
secondViewController.patientName = posts[myIndex].author
更新:感谢 Jose 的回答,它可以正常工作,现在看起来像这样......以防有人遇到和我一样的问题。
// tap image to go at the indexpath of the user
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
print("image tapped")
let pointInCollectionView = tapGestureRecognizer.location(in: collectionView)
let indexPath = collectionView?.indexPathForItem(at: pointInCollectionView)
print(indexPath!)
// this one works - show segue
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "patientProfile") as! PatientProfileVC
secondViewController.userID = posts[(indexPath?.row)!].userID
secondViewController.patientName = posts[(indexPath?.row)!].author
print("Passed data to the other view")
self.show(secondViewController, sender: self)
}
【问题讨论】:
-
你能解释一下
didSelectItemAt有什么问题吗?看起来不错 -
那部分没问题,没有错。我只是无法实现第二个转场,因为它在情节提要中不存在。它没有 segue 标识符。我无法在 if 语句中比较它们,或者我不知道是否有可能。我在 imageTapped 函数中实现它的原因。它转到 PatientProfileVC,但它对以下默认值征税:myIndex 女巫是 0,因此它显示了我只要求 collectionView 的第一行的详细信息。这就是为什么我试图以某种方式将 indexPath 放入 imageTapped 函数@JuicyFruit
-
这是关于我遇到问题的 imageTapped 函数的第二个问题。
标签: ios swift swift3 indexpath