例如一个cell上的点击事件,通过block回调给控制器处理
cell头文件:
#import <UIKit/UIKit.h>
typedef void(^checkMessage)(NSInteger index);
@interface InformationCell : UITableViewCell
@property(nonatomic,copy) checkMessage clickBack;
@end
cell实现文件
#pragma mark - 点击事件
- (void)clickMessage:(UIButton *)button {
NSInteger index = button.tag;
self.clickBack(index);
}
block回调给控制器处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
__weak typeof(self) weakSelf = self;
static NSString *cellId = @"informationCell";
InformationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[InformationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NewsLastModel *model = _lastNewsDataSource[indexPath.row];
cell.model = model;
cell.clickBack = ^(NSInteger index) {
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf checkMessageWithIndex:index model:model];
};
return cell;
}