【问题标题】:Cant detect button action collectionview无法检测到按钮操作 collectionview
【发布时间】:2014-02-08 22:18:18
【问题描述】:

我有一个收藏视图[不是自定义收藏视图],我想通过单击按钮来显示警报。但是按钮操作没有检测到我的代码操作:

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

    }

可能是什么问题谢谢!

【问题讨论】:

标签: ios button uicollectionview buttonclick


【解决方案1】:

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

[cell addSubview:btn];
return cell;

但是你知道 UICollectionView 有一个选择的委托方法吗? collectionView:didSelectItemAtIndexPath:

【讨论】:

  • 是的。但是当我添加 collectionView:didSelectItemAtIndexPath 并单击按钮应用程序崩溃时的问题?另一个细节是集合视图是一个子视图。
【解决方案2】:

哇,你永远不想在cellForItemAtIndexPath: 委托方法中添加这样的按钮,永远!创建一个自定义单元格视图并在其中添加按钮,然后在该代码中创建按钮的实例

然后检查是否首先调用了按钮的选择器方法。您可以在其中添加一个 NSLog 语句来测试正在调用的方法。此外,您应该使用DidSelectItem 委托方法来检测触摸。您提到当您尝试实现 didSelectItem 委托方法时您的代码崩溃了,这不是避免解决问题并在其他地方实现您的选择代码的借口。修复您的崩溃并正确实施它。当您尝试实现 didSelectItem 委托方法时,让我们知道崩溃的实际情况,以便我们可以进一步帮助您。

更新 1 重新创建您的笔尖并需要以下post 中的答案。它指的是您的单元格是 hi 视图而不是 UICOllectionView,这很可能是您的内容视图未接收到触摸事件的原因

【讨论】:

  • 在我看来,OP 对 Obj-C 知之甚少,应该复习一下。提供的代码中有大量错误。接受这个男人的建议并“修复你的崩溃并正确实施它”
  • CollectionCell *cell = [ self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; NSArray *sectionArr=[self.ClassesArr objectAtIndex:indexPath.section]; NSDictionary *data = [sectionArr objectAtIndex:indexPath.row]; [cell.img addTarget:self action:@selector(subCateBtnAction:) forControlEvents:UIControlEventTouchUpInside];返回单元格;在 collectionView:cellForItemAtIndexPath 中,我确实喜欢这个,但我在 collectionview 中看不到任何信息。
  • [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];并且在 viewdidload 中,我注册了自定义单元格但没有结果:S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
相关资源
最近更新 更多