【发布时间】:2017-12-22 15:58:06
【问题描述】:
我有一个集合视图,我正在尝试获取我正在查看和弹出的单元格的索引。
问题
目前我使用的是indexPathForItemAtPoint:,但是无论我在屏幕上的哪个位置点击,这个总是返回0。
代码
集合视图控制器:
- (void)viewDidLoad {
[super viewDidLoad];
[self registerForPreviewingWithDelegate:self sourceView:self.collectionView];
}
- (UIViewController *) previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
CellEditViewController *CEVC = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"]; //The view I want peek/pop to
NSLog(@"Location: %f,%f", location.x, location.y);
NSLog(@"Index of: %lu", [[self.collectionView indexPathForItemAtPoint:location] row]);
[CEVC setPreferredContentSize:CGSizeMake(0.0, 320.0)];
return CEVC;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}
我尝试过的
- 创建一个新位置来识别索引单元格。
- 将
registerForPreviewingWithDelegate:sourceView:移动到我创建每个单元格的位置。 - 将
previewingContext:viewControllerForLocation:和previewingContext:commitViewController:移动到单元格视图方法,由于其他原因,这不起作用。
我不认为这是预览的问题,因为当我使用 UITapGestureRecognizer 实现相同的事情时,我得到了类似的输出:
点击识别器:
- (void) processDoubleTap:(UITapGestureRecognizer *)sender {
NSLog(@"Got tapped twice");
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint point = [sender locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
NSLog(@"Index was: %lu", [indexPath row]);
if (indexPath) {
NSLog(@"Index was double tapped");
}
}
}
输出:
2017-12-25 10:48:13.990523-0800 quickthings[3052:356150] Got tapped twice
2017-12-25 10:48:13.990843-0800 quickthings[3052:356150] Index was: 0
来源
截图
这就是发生的事情,这正是我想要的。我唯一想做的另一件事是当单元格被点击时也能够获得被点击单元格的索引。
故事板中的收藏视图
(蓝色)UIView“链接”到集合视图控制器(下面第二个屏幕截图中的顶部视图控制器)。
【问题讨论】:
-
[self registerForPreviewingWithDelegate:self sourceView:self.collectionview];? (见:stackoverflow.com/questions/33002637/…) -
感谢您的建议,但还是同样的问题 :(
-
我之前没有使用过这个功能,所以我设置了一个非常基本的测试(在 Swift 中,但这不重要),它可以按您的预期工作。这让我相信这是你所展示的东西之外的东西。您是否有更多信息(代码、示例等)可能有助于阐明这一点?
-
@UpholderOfTruth 我已经添加了回购和截图,这有帮助吗?如果您想让我添加任何其他内容,请告诉我。感谢您的帮助!
-
我快速浏览了一下,您的问题在于情节提要中 CollectionViewController 的设置。我才刚进去,已经过了午夜,所以我现在已经厌倦了解释,但我明天会尝试一个(实际上是今天晚些时候)。
标签: ios objective-c cocoa-touch uicollectionview 3dtouch