ios - 使用点击手势时如何获取 UICollectionView header 的索引路径?

标签 ios objective-c ios6 uicollectionview

我使用 locationInView 获取接触点并将其传递给 Collection View 的 indexPathForItemAtPoint。我会得到单元格的索引路径,但永远不会是 UICollectionReusableView(页眉/页脚),因为它总是返回 nil。

最佳答案

header 并没有真正的 indexPath;它报告为第 0 行,但该部分中的第一个单元格也是如此。

您可以通过制作一个具有 Integer 属性的 UITapGestureRecognizer 的简单子(monad)类轻松解决这个问题,只需将以下接口(interface)和空实现放在 View Controller 的 .m 文件的顶部:

@interface HeaderTapRecognizer : UITapGestureRecognizer
@property (nonatomic, assign) NSInteger sectionNumber;
@end

@implementation HeaderTapRecognizer
@end

当您提供补充 View 时,只需添加这些识别器之一并设置节号:

HeaderTapRecognizer *recognizer = [[HeaderTapRecognizer alloc] initWithTarget:self action:@selector(headerTapped:)];
recognizer.sectionNumber = indexPath.section;
[cell addGestureRecognizer:recognizer];

现在您可以访问操作 block 中的节号:

- (void)headerTapped:(id)sender
{
    HeaderTapRecognizer *htr = sender;
    NSInteger sectionNumber = htr.sectionNumber;
    NSLog(@"Header tapped for index Section %d",sectionNumber);
}

关于ios - 使用点击手势时如何获取 UICollectionView header 的索引路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918720/

相关文章:

ios - 在 iPhone 中获取错误的 GPS 位置

ios - 将 IBDesignable 中的操作 UIButton 转发给父 Controller

Objective-C 全局可访问方法

ios - 如何使用 AutoLayout 和 IB 根据设备更改 UITextField 的高度

ios6 - 在 iOS 6.0 和 iOS 6.0.1 上调用 [FBSession openActiveSessionWithPermissions...] 时从 ACAccountStore 抛出 NSInvalidArgumentException

iphone - 将 Iphone 数据发送到 PHP NSUTF8StringEncoding

android - 我可以使用agora_rtc在Flutter中实现一对一语音通话(VOIP)吗?

ios - 删除/重置核心数据中的所有条目?

objective-c - 如何在Objective-C中的原子属性的ivar上创建手动锁定?

ios - 解释 ALAssetRepresentation 中的 XMP 元数据