在 UICollectionViewCell 中为 CollectionView 创建一个句柄
在 UICollectionViewCell 的 .h 文件中
@property (nonataomic, retain) UICollectionView *collView;
在 UICollectionViewCell 的 .m 文件中
@synthesize *collView;
然后在Controller的实现文件中的foll Method中设置Collection View
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homePageCollViewCellIdentifier forIndexPath:indexPath];
//NSString *str = [NSString stringWithFormat:@"HP item %d", indexPath.row+1];
cell.collView = self.theCollectionView;
}
现在在你的 UICollectionViewCell 的实现中
- (void)awakeFromNib
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:button];
}
现在在您的按钮单击方法中
-(void)buttonClicked:(id)sender
{
NSLog(@"button clicked");
NSIndexPath *indPath = [collVw indexPathForCell:self];
[collVw.delegate collectionView:self.collVw didSelectItemAtIndexPath:indPath];
}