ios 如何使用 indexPathsForSelectedItems 在集合视图中获取所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26314852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a selected item in collection view using indexPathsForSelectedItems
提问by jmcastel
I have a collectionView of photos and want to pass the photo who was cliked to a detailViewControler.
我有一个照片的 collectionView 并且想要传递被点击到 detailViewControler 的照片。
The collection data come from :
采集数据来自:
var timeLineData:NSMutableArray = NSMutableArray ()
I would like to use the prepare for segue method.
我想使用准备转场方法。
My problem is how to get the good indexPath from the cell who was clicked ?
我的问题是如何从被点击的单元格中获得好的 indexPath ?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue == "goToZoom" {
let zoomVC : PhotoZoomViewController = segue.destinationViewController as PhotoZoomViewController
let cell = sender as UserPostsCell
let indexPath = self.collectionView!.indexPathForCell(cell)
let userPost = self.timeLineData.objectAtIndex(indexPath!.row) as PFObject
zoomVC.post = userPost
}
}
回答by rdelmar
The sender argument in prepareForSegue:sender: will be the cell if you connected the segue from the cell. In that case you can get the indexPath from the cell,
如果您从单元连接了 segue,prepareForSegue:sender: 中的 sender 参数将是单元。在这种情况下,您可以从单元格中获取 indexPath,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showZoomController" {
let zoomVC = segue.destinationViewController as PhotoZoomViewController
let cell = sender as UICollectionViewCell
let indexPath = self.collectionView!.indexPathForCell(cell)
let userPost = self.timeLineData.objectAtIndex(indexPath.row) as PFObject
zoomVC.post = userPost
}
}
回答by pbasdf
The indexPathsForSelectedItems
returns an array of indexPaths (since there might be several items selected) so you need to use:
在indexPathsForSelectedItems
返回indexPaths数组(因为可能有选择的几个项目),所以你需要使用:
let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath
(You should probably test to see whether several items are selected, and handle accordingly).
(您可能应该测试以查看是否选择了多个项目,并进行相应处理)。
回答by tuandapen
In swift 3:
在快速 3 中:
let index = self.collectionView.indexPathsForSelectedItems?.first
let index = self.collectionView.indexPathsForSelectedItems?.first
回答by fs_tigre
Swift 3.0
斯威夫特 3.0
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == “segueID”{
if let destination = segue.destination as? YourDestinationViewController{
let cell = sender as! UICollectionViewCell
let indexPath = myCollectionView.indexPath(for: cell)
let selectedData = myArray[(indexPath?.row)!]
// postedData is the variable that will be sent, make sure to declare it in YourDestinationViewController
destination.postedData = selectedData
}
}
}
回答by BoSoud
Objective-c:
目标-c:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
PhotoZoomViewController *zoomVC = [segue destinationViewController];
NSIndexPath *path = [self.collectionView indexPathForCell:(sender)];
NSDictionary *dataObj = [timeLineData objectAtIndex:path.row];
// In PhotoZoomViewController create NSDictionary with name myDictionary
// in this line will copy the dataObj to myDictionary
zoomVC.myDictionary = dataObj;
}
Swift:
斯威夫特:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let zoomVC = segue.destinationViewController as PhotoZoomViewController
let cell = sender as UICollectionViewCell
let indexPath = self.collectionView!.indexPathForCell(cell)
let userPost = self.timeLineData.objectAtIndex(indexPath.row) as PFObject
zoomVC.post = userPost
}