在 UICollectionView 中实现按钮点击 [英] Achieve button click in UICollectionView
本文介绍了在 UICollectionView 中实现按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从 UICollectionViewCell
中的按钮获取按钮单击事件?我用笔尖填充集合视图,单元格有按钮,但它的动作没有被调用.我认为问题在于被调用的代表.我该如何解决这个问题?
Is there a way I can get the button click event from a button inside a UICollectionViewCell
? I used a nib to populate the collection view, the cell has the button but its action is not getting called. I think the problem is with the delegate being called. How can I fix this?
我是如何创建的:
- 添加了一个空笔尖,创建了一个集合视图单元格
- 添加了一个 .h 和 .m 文件,并将单元格 nib 的文件所有者作为创建的类</li>
- 在课堂上写了一个动作.
- 将按钮连接到操作
有没有办法让我采取行动?我做错了什么?
Is there a way I can get the action? What am I doing wrong?
推荐答案
像这样添加按钮动作:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellId" forIndexPath:[indexPath row]];
[[cell myButton] addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)myClickEvent:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:_myCollectionArray];
NSIndexPath *indexPath = [_myCollectionArray indexPathForItemAtPoint: currentTouchPosition];
}
这篇关于在 UICollectionView 中实现按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文