IOS--在cell里面用代理实现和block一样的功能

1. ....cell.h

@class HMDownloadTableViewCell;

@protocol HMDownloadTableViewCellDegate <NSObject>

- (void)downloadTableViewCellNeedDelete:(HMDownloadTableViewCell *)cell;

@end

@interface HMDownloadTableViewCell : UITableViewCell
@property (nonatomic, weak) id<HMDownloadTableViewCellDegate> delegate;
@property (strong, nonatomic)  UIButton *deleteBtn;                     //删除按钮
@end

2.   ...cell.m

//触发,这里是通过cell里面的按钮来触发的
- (void)deleteBtnClick:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(downloadTableViewCellNeedDelete:)]) {
        [self.delegate downloadTableViewCellNeedDelete:self];
    }
}

3.  ...tableView.m

<UITableViewDelegate, UITableViewDataSource, HMDownloadTableViewCellDegate>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  .....
  cell.delegate = self;    
   ....
}

#pragma mark - HMDownloadTableViewCellDegate
- (void)downloadTableViewCellNeedDelete:(HMDownloadTableViewCell *)cell {
    NSLog(@"删除cell的书籍名字%@",cell.titleLabel.text);
}
原文地址:https://www.cnblogs.com/qiyiyifan/p/7305531.html