【发布时间】:2015-11-18 17:50:26
【问题描述】:
我有一个 UICollectionView,它的所有单元格都布置好了。
我已将此声明为子视图:
@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) 是必需的,因为单元格可以重复使用。
我现在如何进行这项工作?谢谢。
【问题讨论】:
-
究竟出了什么问题?除非您在其他地方删除按钮,否则 nil 检查与重用无关。
-
你为什么不使用storyboard作为collectionview和prototype cell呢?更好的方法是在storyboard上设置按钮。
-
框架在 // add all aButton stuff here 中设置得很好且正确
标签: ios objective-c uibutton uicollectionview uicollectionviewcell