简体   繁体   English

UICollectionView单元的自定义委托-自定义按钮不起作用

[英]Custom Delegate For UICollectionView Cell - Custom Button Not working

I have a custom cell Subclassed from UIcollectionViewCell . 我有一个从UIcollectionViewCell子类化的自定义单元格。 Through code i have created buttons and added to the cell with target method in the same class. 通过代码,我创建了按钮,并使用同一类中的target方法将其添加到单元格中。

button event is working fine . 按钮事件运行正常。 But i need to take control to my base view where i created UICollectionView. 但是我需要控制创建UICollectionView的基本视图。

So for that i have created custom delegate for recognizing the tap event. 为此,我创建了用于识别tap事件的自定义委托。

---> DBDraggingCell.h file ---> 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

It seems that you didn't set cell delegate in your collectionView 似乎您没有在collectionView中设置单元格委托

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DBDraggingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:DBDraggingCellIdentifier forIndexPath:indexPath];

    cell.delegate = self;

    ....

}

Your base view is UICollectionView instance, isn't it? 您的基本视图是UICollectionView实例,不是吗? If your base view is UICollectionView, you have to set DBDraggingCell's delegate in the loading cells method - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 如果您的基本视图是UICollectionView,则必须在加载单元方法中设置DBDraggingCell的委托- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

For Example: 例如:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        DBDraggingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YOUR_CELL_IDENTIFIER forIndexPath:indexPath];

        // Set DBDraggingCell' delegate here
        cell.delegate = self;

 }

Make sure that your delegate property corresponds to the variable _delegate, which you used when you perform the delegate method. 请确保您的委托属性与执行委托方法时使用的变量_delegate对应。 It is better to use the property everywhere that could helps you to avoid those kind of issues. 最好在所有可以帮助您避免此类问题的地方使用该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM