在UICollectionView中实现按钮单击

Lit*_*T.V 5 delegates ipad ios uicollectionview uicollectionviewcell

有没有办法从一个按钮内部获取按钮点击事件UICollectionViewCell?我用一个笔尖来填充集合视图,单元格有按钮但是它的动作没有被调用.我认为问题在于调用委托.我怎样才能解决这个问题?

我是如何创造的:

  1. 添加了一个空的笔尖,创建了一个集合视图单元格
  2. 添加了.h和.m文件,并将单元格nib的文件所有者作为创建的类
  3. 在课堂上写了一个动作.
  4. 将按钮连接到动作

有没有办法让我采取行动?我究竟做错了什么?

Mac*_*ark 20

通过从"对象"面板拖动"集合视图单元",在Nib中创建单元格非常重要.如果您使用UIView并在Identity Inspector中更改此单元格的类,则该操作将不起作用.


小智 10

像这样添加按钮动作:

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

}
Run Code Online (Sandbox Code Playgroud)