首页 文章

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

提问于
浏览
2

我有一个来自UIcollectionViewCell的自定义单元格Subclassed . 通过代码,我创建了按钮,并在同一个类中使用target方法添加到单元格中 .

按钮事件工作正常 . 但我需要控制到我创建UICollectionView的基本视图 .

所以为此我创建了自定义委托来识别点击事件 .

---> 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

3 回答

  • 0

    您似乎没有在collectionView中设置单元格委托

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        DBDraggingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:DBDraggingCellIdentifier forIndexPath:indexPath];
    
        cell.delegate = self;
    
        ....
    
    }
    
  • 1

    您的基本视图是UICollectionView实例,加载单元格方法中的isn 't it? If your base view is UICollectionView, you have to set DBDraggingCell' s委托 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    例如:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
            DBDraggingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YOUR_CELL_IDENTIFIER forIndexPath:indexPath];
    
            // Set DBDraggingCell' delegate here
            cell.delegate = self;
    
     }
    
  • 5

    确保您的delegate属性对应于您在执行委托方法时使用的变量_delegate . 最好在所有地方使用可以帮助您避免这类问题的 property .

相关问题