获取button操作:UICollectionView单元格

我用nib创build了一个UICollectionViewCell ,并在其中添加了一个button,并创build了一个.h和.m文件,将这个类添加到了nibs file's owner中。然后在.m中写入一个button动作,通过插口连接它。

收集视图填充正常,但无法触发button操作。 我认为收集单元的代表被调用。

我怎样才能得到button的动作?

我也有这个问题。 没有子视图会收到触摸事件。 虽然斯科特K的解决方法确实有效,但我仍然觉得有些不妥。 所以我再看看我的笔尖,注意到我用来创build一个UICollectionViewCell的原始子视图是一个UIView。 即使我将该类更改为UICollectionViewCell的子类,XCode仍将其视为UIView,因此,您在contentView中看到的问题不会捕获触摸事件。

为了解决这个问题,我通过确保拖动一个UICollectionViewCell对象并将所有的子视图移动到那个地方来重新标记了笔尖。 之后,触摸事件开始在我的单元格的子视图上工作。

可以指示器来查看你的笔尖是否被configuration为UICollectionViewCell是看你的高级视图的图标。

在这里输入图像说明

如果不是这样,那么它可能会解释触摸事件是错误的。

当通过笔尖创buildUICollectionViewCell时,笔尖的内容不会添加到单元格的contentView中,而是直接添加到UICollectionViewCell中。 似乎没有办法让Interface Builder将nib中的顶层视图识别为UICollectionViewCell,所以“自动”内的所有内容都被添加到contentView中。

正如sunkehappy指出的,任何你想要接收触摸事件都需要进入contentView。 它已经为你创build了,所以你可以做的最好的方式是编程方式将你的UIButton移动到awakeFromNib-time的contentView中。

 -(void)awakeFromNib { [self.contentView addSubview:self.myButton] } 

UICollectionViewCell类参考

要configuration单元格的外观,请添加将contentView中的数据项目内容作为子视图呈现给contentView属性中的视图所需的视图。 不要直接将子视图添加到单元格本身。 单元pipe理多层内容,其中内容视图只有一个。 除了内容视图之外,单元还pipe理两个背景视图,这些背景视图以单选状态和未选状态显示单元格。

你可以像这样在awakeFromNib添加你的button:

 - (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"); } 

我刚刚解决它与添加

 [self bringSubviewToFront:myButton]; 

进入awakeFromNib

我有一个类似的问题,在单元格底部的子视图没有收到触摸事件,但顶部工作正常。 所以我开始调查了,得到了以下结果:

  • 界面构build器会将您创build的单元格的所有子视图添加到单元格的contentView中,即使contentView本身在界面构build器中不可见
  • 我的代码扩展了单元格以适应内容的大小,因此集合视图中的大部分单元格的高度都比Interface Builder中的蓝图高
  • 由于某些原因,单元格本身的“Autoresize子视图”属性被设置为NO。 这导致神秘的和不可见的界面内build的contentView保持原来在界面生成器中的大小,所以在contentView边界之外的任何子视图都没有得到触摸并且没有反应

将Interface Builder中的单元格的“Autoresize子视图”设置为YES解决了我的问题!

在UICollectionViewCell中为CollectionView创build一个句柄

在UICollectionViewCell的.h文件中

 @property (nonataomic, retain) UICollectionView *collView; 

在UICollectionViewCell的.m文件中

 @synthesize *collView; 

然后在foll方法的控制器的实现文件中设置集合视图

 -(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]; } 

现在在你的button单击的方法

 -(void)buttonClicked:(id)sender { NSLog(@"button clicked"); NSIndexPath *indPath = [collVw indexPathForCell:self]; [collVw.delegate collectionView:self.collVw didSelectItemAtIndexPath:indPath]; }