0

我有一个UICollectionView,其单元全部布局。在UICollectionViewCell中点击一个UIButton

我有这个声明为一个子视图:

@property (nonatomic, strong) UIButton *aButton; 

然后我有一个声明在每个单元中,像这样:

if (_aButton == nil) 
{ 
    _aButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
} 

// Add in all _aButton info here 

[self.contentView addSubview:_aButton]; 

// Call to button pressed for button 

[_aButton addTarget:self action:@selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

点击按钮的方法是像这样:

- (IBAction) aButtonPressed:(UIButton *) sender 
{ 
    // Code never gets heree 
} 

if(_aButton ==`nil)是需要的,因为单元格被重用。

现在该如何完成这项工作?谢谢。

+0

究竟是什么错误?零检查与重用无关,除非您将按钮移到其他地方。 – Wain

+1

为什么不使用故事板for collectionview与原型单元格?更好的方式来设置故事板上的按钮。 – merge

+0

框架设置正确,并在//中添加所有aButton的东西 – cdub

回答

0

在按钮添加到视图之前添加按钮操作代码....可能会有效。

// Call to button pressed for button 

[_aButton addTarget:self action:@selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

// Add in all _aButton info here 

[self.contentView addSubview:_aButton]; 
+0

不知道为什么,但该开关允许按钮被调用。 – cdub

0

我想,你初始化按钮的方式会让你的帧为0,0,0,0。所以首先给它一个适当的框架和标题,以便在屏幕上可见。

然后尝试点击该标题,看看它是否有效。

0

目前还不清楚你想在这里实现什么,但按钮不起作用,因为你没有设置它在单元格上的位置和大小。这可以通过设置框架或布局约束来完成。

尝试设置按钮框:

_aButton.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f); 

您还可以设置背景颜色按钮,只是为了它的细胞可见:

_aButton.backgroundColor = [UIColor redColor]; 

一切是正确的,按钮应工作。

相关问题