【问题标题】:Button click method is not working in collection view按钮单击方法在集合视图中不起作用
【发布时间】:2016-04-10 12:15:59
【问题描述】:

我正在创建一个相机应用程序。我在收藏视图中显示捕获的图片。我放置了一个按钮来删除特定的图片。运行时,我可以看到要删除的按钮,但如果我单击该按钮,它不会执行任何操作。

-(UICollectionViewCell *)collectionView:(UICollectionView *)
collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    Cell.self.image_View.image=[self.imageArray objectAtIndex:indexPath.row];

    UIButton *deleteButton = [[UIButton alloc]init];
    deleteButton.frame = CGRectMake(80, 0, 20, 20);
    //deleteButton.backgroundColor = [UIColor redColor];
    [deleteButton setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
    [deleteButton setTag:indexPath.row];

    [deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];

    [Cell.self.image_View addSubview:deleteButton];

    return Cell;
 }

 -(void) delete:(UIButton*)sender{
      UIButton *btn = (UIButton *)sender;
      [self.imageArray removeObjectAtIndex:btn.tag];
      //reload your collectionview here
 }

谁能帮帮我?

【问题讨论】:

  • 为什么要在 UIImageView 上添加删除按钮?将其添加到单元格中。也尝试添加按钮,如 UIButton *deleteButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];或 UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  • 感谢 Arun Gupta,它成功了,我只需将我的按钮添加到我的单元格中

标签: objective-c xcode7 collectionview


【解决方案1】:

我想你可能错过了

[collectionView reloadData] 


    -(void) delete:(UIButton*)sender{
    UIButton *btn = (UIButton *)sender;
   [self.imageArray removeObjectAtIndex:btn.tag];
    [collectionView reloadData] 

}

【讨论】:

  • 谢谢伊曼,我知道我想重新加载集合视图。如果我在删除方法中设置了一个断点,它就不会按照那个方法进行。我不知道为什么
  • 啊好吧,方法应该是这样的 (IBAction) delete:(UIButton*)sender 而不是 void
  • 伊曼,如果我使用 IBAction 方式,该按钮应该在故事板右侧创建,我仅以编程方式创建按钮
猜你喜欢
  • 2018-08-13
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 2020-10-05
  • 2014-09-17
  • 2023-04-05
相关资源
最近更新 更多