我有一个从 UIcollectionViewCell 子类化的自定义单元格。通过代码,我创建了按钮并使用同一类中的目标方法将其添加到单元格中。
按钮事件工作正常。但是我需要控制我创建 UICollectionView 的基本 View 。
为此,我创建了用于识别点击事件的自定义委托(delegate)。
---> DBDraggingCell.h 文件
@protocol DBDraggingCellDelegate <NSObject>
-(void)chartButtonpressed:(id)sender;
-(void)accountHistoryButtonpressed:(id)sender;
-(void)transactionHistoryButtonpressed:(id)sender;
@end
@interface DBDraggingCell : UICollectionViewCell{
UIButton *chartButton;
UIButton *accSummaryButton;
UIButton *transactionHistory;
}
/////////////////////////////////////////
-(void)chartPressed:(UIButton *)sender{
[_delegate chartButtonpressed:sender];
}
_delegate returns nil
----> In baseview i have set the delegate
[self addSubview:_theCollectionView];
_theCollectionView.delegate=self;
Not working
The methods inside the baseview not called
最佳答案
看来您没有在 collectionView 中设置 cell delegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
DBDraggingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:DBDraggingCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
....
}
关于ios - UICollectionView 单元格的自定义委托(delegate) - 自定义按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25238757/