在UICollectionView中实现按钮 [英] Achieve button click in UICollectionView

查看:115
本文介绍了在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?

我如何创建:


  1. nib创建一个集合视图单元格

  2. 添加了一个.h和.m文件,并将单元格nib的文件所有者作为创建的类<​​/ li>

  3. 将按钮连接到动作

有没有办法可以得到动作吗?
我做错了什么?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆