ios - 无法检测按钮 Action Collection View

标签 ios button uicollectionview buttonclick

我有一个 Collection View [不是自定义 Collection View ],我想通过单击按钮显示一个警报。但是按钮 Action 没有检测到我的代码的 Action :

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

        UICollectionViewCell *cell = [ self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

        NSArray *sectionArr=[self.ClassesArr objectAtIndex:indexPath.section];

        NSDictionary *data =  [sectionArr objectAtIndex:indexPath.row];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(54, 25, 15, 15);
        [btn addTarget:self action:@selector(subCateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];

    }


// the button action
    -(IBAction)subCateBtnAction:(UIButton *)btn
    {
        NSArray *sectionArr=[self.ClassesArr objectAtIndex:sectionindex];

        NSDictionary *data =  [sectionArr objectAtIndex:btn.tag];
        NSString *name = [data objectForKey:@"nombreTarifa"];
        UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@""
                                                             message:name
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
        [Notpermitted show];

    }

可能是什么问题谢谢!

最佳答案

看起来您没有将按钮添加到单元格或返回单元格,试试这个:

[cell addSubview:btn];
return cell;

但是您是否意识到 UICollectionView 有一个用于选择的委托(delegate)方法? collectionView:didSelectItemAtIndexPath:

关于ios - 无法检测按钮 Action Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222253/

相关文章:

ios - Swift - 从字典中提取数据

html - CSS:删除禁用的 HTML 按钮上的阴影

javascript - 如何从按钮调用 Javascript 中的方法(使用此方法)?

ios - 如何处理 UICollectionView 中的后台点击

ios - 在 UICollectionView (SWIFT) 中设置默认单元格?

ios - setNeedsDisplayInMapRect - 已弃用的 iOS7

ios - 我该如何修复或解决此 tableView textField 自动布局故障?

ios - 将 UIScrollView Delegate 和 UICollectionView Delegate 分配给同一个类

iOS 图像缓存与内存中图像的自定义计数

asp.net - 在 asp :Button 中换行文本