ios - UICollectionView 单元格的自定义委托(delegate) - 自定义按钮不起作用

标签 ios objective-c uicollectionview uicollectionviewcell

我有一个从 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/

相关文章:

iphone - 更改 UIView 的位置不起作用

ios - 是否可以根据国家/地区更改 iPhone 应用程序描述措辞

具有动态高度的 UICollectionViewLayout - 但不使用流布局

ios - 按钮状态不会循环更改

ios - Crashlytics 不会捕捉到内存不足

ios - Watchkit 中元素大小相对于屏幕大小

ios - Objective-C : Why can't I update a label from another class (AppDelegate > MainViewController)?

ios - 如何在一个 UITableView 中制作多个列表? iOS swift

ios - cellForItemAtIndexPath 在 block 中获取错误的 indexPath 值

swift - CollectionView,两个按钮无法通信 - Swift 4