【发布时间】:2013-02-17 06:52:44
【问题描述】:
我使用 locationInView 获取接触点并将其传递给集合视图的 indexPathForItemAtPoint。我将获得一个单元格的索引路径,但绝不是 UICollectionReusableView(页眉/页脚),因为它总是返回 nil。
【问题讨论】:
标签: ios objective-c ios6 uicollectionview
我使用 locationInView 获取接触点并将其传递给集合视图的 indexPathForItemAtPoint。我将获得一个单元格的索引路径,但绝不是 UICollectionReusableView(页眉/页脚),因为它总是返回 nil。
【问题讨论】:
标签: ios objective-c ios6 uicollectionview
标头实际上没有 indexPath;它报告为第 0 行,但该部分中的第一个单元格也是如此。
您可以通过创建一个具有 Integer 属性的简单 UITapGestureRecognizer 子类轻松解决此问题,只需将以下接口和空实现放在 View Controller 的 .m 文件的顶部即可:
@interface HeaderTapRecognizer : UITapGestureRecognizer
@property (nonatomic, assign) NSInteger sectionNumber;
@end
@implementation HeaderTapRecognizer
@end
当您提供补充视图时,只需添加这些识别器之一并设置部分编号:
HeaderTapRecognizer *recognizer = [[HeaderTapRecognizer alloc] initWithTarget:self action:@selector(headerTapped:)];
recognizer.sectionNumber = indexPath.section;
[cell addGestureRecognizer:recognizer];
现在您可以访问操作块中的部分编号:
- (void)headerTapped:(id)sender
{
HeaderTapRecognizer *htr = sender;
NSInteger sectionNumber = htr.sectionNumber;
NSLog(@"Header tapped for index Section %d",sectionNumber);
}
【讨论】:
在这方面提供帮助可能为时已晚,但也许其他人会像我一样做到这一点。问题是标头没有有意义的 indexPath(它似乎总是返回 0,0)。
无论如何,当我明白这一点时,我正在检查它是否在标题的子视图内,而不是 indexPath:
CGPoint point = [sender locationInView:collectionView];
if (CGRectContainsPoint(CGRectMake(0.0f,0.0f,140.0f,140.0f), point))
NSLog(@"Point was inside header");
这仅在我的实例中有效,因为我知道标题的大小并且可以安全地假定它在 collectionview 中的位置,因为 collectionView 只有一个部分 (0)。
HTH
【讨论】:
我会创建一个UITapGestureRecognizer 并将其附加到每个标题视图。另一种选择是为每个标题视图提供UIControl 的自定义子类。
【讨论】: