【问题标题】:Custom Delegate For UICollectionView Cell - Custom Button Not workingUICollectionView 单元格的自定义委托 - 自定义按钮不起作用
【发布时间】:2014-08-11 08:22:52
【问题描述】:

我有一个从 UIcollectionViewCell 子类化的自定义单元格。通过代码,我创建了按钮并使用同一类中的目标方法添加到单元格中。

按钮事件工作正常。但我需要控制我创建 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

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您的基本视图是 UICollectionView 实例,不是吗? 如果你的基础视图是 UICollectionView,你必须在加载单元方法中设置 DBDraggingCell 的委托- (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;
      
       }
      

      【讨论】:

        【解决方案3】:

        确保您的委托属性对应于您在执行委托方法时使用的变量_delegate。最好在可以帮助您避免此类问题的任何地方使用该属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-07
          相关资源
          最近更新 更多