- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已将 UILongPressGestureRecognizer
添加到我的 UICollectionViewCell
并创建了一个转场,因此通过长按一个 cell
用户转场到一个 View Controller
。
我已经有一个普通的 segue 使用 didSelect
方法显示单元格详细信息,但是辅助长按手势 segue 会将用户带到操作屏幕以删除单元格。
不幸的是,当使用 prepareForSegue
将单元格数据发送到 destinationViewController
时,我无法使用标准的 let indexPaths = self.collectionView.indexPathForSelectedRow()
方法,因为我在使用手势识别器时正在创建自己的选择。
那么如何使用 gestureRecognizer
将单元格数据发送到 destinationViewController
和 prepareForSegue
?
if segue.identifier == "cellAction" {
let vc = segue.destinationViewController as! CellActionViewController
//This will cause an 'array index out of range error
let indexPaths = self.collectionView.indexPathsForSelectedItems()
let indexPath = indexPaths[0] as! NSIndexPath
//Here is how I would send the cell data
let selectedCell = Items[indexPath.row]
vc.selectedItem = selectedCell
}
最佳答案
因此您需要访问被按下的特定单元格,并使用此信息从 prepareForSegue(_:sender:)
中获取其索引路径。
首先要注意的是 prepareForSegue
方法中的 sender
参数。我假设您正在从手势处理函数调用 performSegueWithIdentifier(_:sender:)
。请注意,它还有一个 sender
参数,它与传递给 prepareForSegue
的对象相同。您可以使用它来传递您的单元实例。
要注意的第二件事是 UIGestureRecognizer
有一个 view
属性,它很可能是单元格本身。有了这些知识,您就可以将按下的单元格实例传递给 prepareForSegue
:
func handleLongPress(longPress: UILongPressGestureRecognizer) {
if longPress.state == .Ended {
performSegueWithIdentifier("cellAction", sender: longPress.view)
}
}
现在,在 prepareForSegue
中,您可以将 sender
转换为您的单元类,并使用 UICollectionView
的 indexPathForCell( _:)
方法:
override prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "cellAction" {
let vc = segue.destinationViewController as! CellActionViewController
if let cell = sender as? MyCell {
let indexPath = collectionView.indexPathForCell(cell)
let selectedItem = items[indexPath.row]
vc.selectedItem = selectedItem
} else {
fatalError("Invalid sender, was expecting instance of MyCell.)
}
}
}
关于ios - 使用 prepareForSegue 将 Cell 数据发送到 destinationViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020339/
我正在使用模板主从应用程序。我从 SplitViewController 添加了一个模态 segue,并为其指定了标识符“DisplayLoginView”。 我从我的 detailViewContr
我很好奇这个方法 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender; 我在 UIViewController.h
我有一个 UINavigationController有一个 UITableview装在里面。里面这个UITableView我有一个 prototype细胞,我用静态数据重用了几次,这些数据永远不会改
我要函数,它们都触发performSegueWithIdentifier以同样的方式。但是根据调用哪个函数,我需要在 prepareForSegue 中传递不同的参数. 就像是 func first(
我已经有一段时间没有进行任何 iOS 编程了,所以请原谅我这个简单的问题。我已经搜索过了,也许我只是想多了。 我有一个名为 ShowStreamDetails 的 segue,将动态原型(protot
当我通过prepareForSegue将数据从FirstViewController传递到SecondViewController( TableView )时,我在secondViewControll
我的应用有核心数据和两个 View CoursesTableViewController 和 DetailViewController。 如果用户点击 Detail Disclosure 按钮转到 D
我有一个带有嵌入式导航 Controller 的 Storyboard (xcode5)(导航 Controller 指向我的主视图 Controller )。 我已经为我想通过我的代码打开的新 Vi
我的应用程序有一个非常奇怪的问题。我在我的应用程序中使用 AFNetworking 框架。我正在加载一些带有图像的提要,然后单击操作打开带有详细描述和大图像的新 View 。所以我将 IdPhoto
我设置了一个 View ,用户可以在其中选择一张照片,然后将其用作个人资料照片。一旦他们选择了图像,就应该更新 UIImageView 。我使用 prepareForSegue 方法将信息传递给包含
我有以下 3 种使用 Storyboard的方法,其中我有一个从“查看电影信息”按钮到下一个 View 的“segue”连接。标识符是“web” 当我单击按钮时,我调用此方法:(IBAction)sy
在我的应用程序中有一个 UITableView 显示 Core Data 对象。当用户点击一行时,必须显示所选对象的详细 View 。详细 View 在 EditToDoItemViewControl
我正在使用模态转场将数据从 SignUpViewController 传递到 RAVerificationViewController。出于某种原因,我的姓名、密码、电话和电子邮件值为零,但宿舍和角色
我确定我有两个 View Controller ,它们有适当的文件作为它们的类。 (ViewController1.swift 和 ViewController2.swift)我试图使用以下代码将数据
我在使用 prepareforSegue 的 View Controller 之间传递字符串 (StringVar) 时遇到问题。我已经研究了关于这个主题的各种教程,但我仍然没有成功(我是新手,所以问
我使用 prepareForSegue 方法将 location_id 从 LocationTableViewController 传递到 BeerTableViewController,以便在点击位
我正在使用 SWRevealViewController 构建侧边栏菜单。从菜单(UITableView)中选择一个元素时,我有选择正确 Controller 的方法。 segue 标识符/名称在 S
我正在从 Storyboard的世界转向没有 Storyboard的世界。我想知道在这种情况下如何实现 prepareForSegue 方法。因为此方法使用 segue 标识符,而我知道提供 segu
我想在 prepareForSegue 中运行一些代码。问题是我正在以编程方式更改 View ,因此未运行 prepareForSegue 函数。我也不知道如何运行它,因为我不能给它一个标识符。 准备
我正在制作一个表格 View ,其中包含 channel ,用户可以点击该 channel 进入该 channel 。当用户点击该 channel 时,我想从 firebase 获取 channel
我是一名优秀的程序员,十分优秀!