ios 获取按钮操作:UICollectionView Cell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13757762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:27:26  来源:igfitidea点击:

Getting button action : UICollectionView Cell

iosipaduicollectionviewuicollectionviewcell

提问by Lithu T.V

I have created a UICollectionViewCellby nib and added a button inside it and created a .h and .m files added the class to the nibs file's owner.then wrote a button action in the .m connected it via outlet.

我创建了一个UICollectionViewCellby 笔尖并在其中添加了一个按钮,并创建了一个 .h 和 .m 文件,将类添加到笔尖file's owner。然后在通过插座连接它的 .m 中编写了一个按钮操作。

The collection view is populating fine ,but cannot get the buton action triggered. I think the delegate for collection cell is called.

集合视图填充良好,但无法触发按钮操作。我认为集合单元的委托被调用。

How can i get the button action?

我怎样才能获得按钮动作?

回答by haider

I had this problem as well. No subviews would receive touch events. While Scott K's workaround does work, I still felt something was wrong. So I took another look at my nib, and noticed that the original subview I used to create a UICollectionViewCell was a UIView. Even though I changed the class to a subclass of UICollectionViewCell, XCode still considered it a UIView, and hence the issues you see with contentView not catching touch events.

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

To fix this, I redid the nib by making sure to drag a UICollectionViewCell object, and moving all the subviews to that. Afterwards, touch events began to work on my cell's subviews.

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

Could indicator to see if your nib is configured as a UICollectionViewCell is look at the icon for your high level view.

可以查看您的笔尖是否配置为 UICollectionViewCell 的指示器是查看您的高级视图的图标。

enter image description here

在此处输入图片说明

If it doesn't look like this, then its probably going to interpret touch events wrong.

如果它看起来不像这样,那么它可能会错误地解释触摸事件。

回答by Scott K.

When you create a UICollectionViewCellvia a nib the contents of the nib are not added to the cell's contentView -- it all gets added directly to the UICollectionViewCell. There doesn't appear to be a way to get Interface Builder to recognize the top-level view in the nib as a UICollectionViewCell, so all of the contents inside 'automatically' get added to the contentView.

当您UICollectionViewCell通过笔尖创建一个时,笔尖的内容不会添加到单元格的 contentView - 所有这些都直接添加到UICollectionViewCell. 似乎没有办法让 Interface Builder 将笔尖中的顶级视图识别为 a UICollectionViewCell,因此“自动”将所有内容添加到 contentView 中。

As sunkehappy pointed out, anything that you want to receive touch events needs to go into the contentView. It's already been created for you, so the best you can do is to programmatically move your UIButtoninto the contentView at awakeFromNib-time.

正如 sunkehappy 指出的那样,您想要接收触摸事件的任何内容都需要进入 contentView。它已经为您创建好了,所以您能做的最好的事情就是UIButton在awakeFromNib 时间以编程方式将您的内容移动到contentView 中。

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

回答by sunkehappy

UICollectionViewCell Class Reference

UICollectionViewCell 类参考

To configure the appearance of your cell, add the views needed to present the data item's content as subviews to the view in the contentView property. Do not directly add subviews to the cell itself. The cell manages multiple layers of content, of which the content view is only one. In addition to the content view, the cell manages two background views that are display the cell in its selected and unselected states.

要配置单元格的外观,请将数据项的内容作为子视图呈现所需的视图添加到 contentView 属性中的视图。不要直接向单元格本身添加子视图。单元格管理着多层内容,内容视图只是其中的一层。除了内容视图之外,单元格还管理两个背景视图,它们以选中和未选中状态显示单元格。

You can add your button in awakeFromNiblike this:

您可以awakeFromNib像这样添加按钮:

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

回答by kalafun

I've just solved it with adding

我刚刚通过添加解决了它

[self bringSubviewToFront:myButton];

into awakeFromNib

进入 awakeFromNib

回答by Zoltán Lippai

I had a similar issue where subviews at the bottom part of the cell didn't receive touch events, but the top part was working fine. So I've started to investigate, and got the following results:

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

  • The interface builder will add any subviews of the cell you create in it to the contentView of the cell, even though the contentView itself is not visible in the interface builder
  • My code expanded the cells to suit the size of the content, so the majority of the cells in the collection view were of larger height than the blueprint in Interface Builder
  • For some reason, the 'Autoresize subviews' property of the cell itself was set to NO. This caused mysterious and not-visible-in-interface-builder contentView to remain of the same size as the cell had in interface builder originally, so any subviews that were outside the bounds of the contentView did not receive touches and were unresponsive
  • 界面构建器会将您在其中创建的单元格的任何子视图添加到单元格的 contentView 中,即使 contentView 本身在界面构建器中不可见
  • 我的代码扩展了单元格以适应内容的大小,因此集合视图中的大多数单元格的高度都大于界面生成器中的蓝图
  • 出于某种原因,单元格本身的“自动调整子视图大小”属性设置为 NO。这导致神秘且不可见的界面构建器 contentView 保持与界面构建器中单元格最初相同的大小,因此在 contentView 边界之外的任何子视图都没有收到触摸并且没有响应

Setting the 'Autoresize subviews' of the cell in Interface Builder to YES solved my problem!

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

回答by R. Mohan

I feel hard to understand the Accepted answer, I will try to give a simple answer.

我觉得很难理解接受的答案,我会尽量给出一个简单的答案。

In UICollectionViewCell there are two types.

在 UICollectionViewCell 中有两种类型。

  1. Collection View Cell
  2. Collection Reusable View
  1. 集合视图单元格
  2. 集合可重用视图

I used the Collection Reusable View, in that the button actions are not working.

我使用了集合可重用视图,因为按钮操作不起作用。

Then as per the accepted answer i tried to use the Collection View Cell, in that only the Button Actions are Working. Use the second object in the Image. It will work fine.

然后根据接受的答案,我尝试使用集合视图单元格,因为只有按钮操作有效。使用图像中的第二个对象。它会正常工作。

enter image description here

在此处输入图片说明

回答by shreyas1811

Create a Handle for the CollectionView in the UICollectionViewCell

在 UICollectionViewCell 中为 CollectionView 创建一个句柄

In the .h file of the UICollectionViewCell

在 UICollectionViewCell 的 .h 文件中

@property (nonataomic, retain) UICollectionView *collView;

In the .m file of the UICollectionViewCell

在 UICollectionViewCell 的 .m 文件中

@synthesize *collView;

Then in the implementation File of the Controller in the foll Method set the Collection View

然后在 foll 方法中 Controller 的实现 File 中设置 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;
}

Now in the implementation of your UICollectionViewCell

现在在你的 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];
}

Now in your Button Clicked Method

现在在你的按钮点击方法中

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