如何将事件添加到UICollectionView单元格?

Ser*_*gey 2 cocoa-touch objective-c target-action uicollectionview

我想处理点击UICollectionView单元格.尝试使用以下代码实现此目的:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *cellIdentifier = @"cvCell";    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];  

    // Some code to initialize the cell

    [cell addTarget:self action:@selector(showUserPopover:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)showUserPopover:(id)sender
{
     //...
}
Run Code Online (Sandbox Code Playgroud)

但执行中断[cell addTarget:...]了以下错误:

- [UICollectionViewCell addTarget:action:forControlEvents:]:无法识别的选择器发送到实例0x9c75e40

Top*_*lou 19

您应该实现UICollectionViewDelegate协议,您将找到该方法:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

告诉您用户何时触摸一个单元格