引言:
现在有一个需求, 如果一个自定义cell中有一个button, button的点击事件要将自定义cell中的某个属性值传给控制器, 应该怎么做?
当然你可以利用代理, 通知, 和block回调, 除此之外, 还有没有其他办法呢? 有! 那就是今天要说的路由响应链方法,避免了重复代码及代码美观性
直接上代码
UIResponder的类目:
@interface UIResponder (YJRouter)
- (void)routerWithEventName:(NSString *)eventName modelInfo:(NSDictionary *)modelInfo;
@end
<p>#import "UIResponder+Router.h"
@implementation UIResponder (Router)
- (void)routerWithEventName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
if (self.nextResponder) {
[[self nextResponder] routerWithEventName:eventName userInfo:userInfo];
}
}
@end
Cell类:
#import <UIKit/UIKit.h>
#import "YJButtonModel.h"
static NSString *const TransferNameEvent = @"TransferNameEvent";
static NSString *const ButtonTag = @"ButtonTag";
static NSString *const ModelInfo = @"ModelInfo";
@interface YJButtonTableViewCell : UITableViewCell
@property (strong, nonatomic) YJButtonModel *model;
@end
‘#import "YJButtonTableViewCell.h"
@interface YJButtonTableViewCell ()
@property (strong, nonatomic) UIButton *sureButton;
@property (strong, nonatomic) UIButton *deleteButton;
@property (strong, nonatomic) YJButtonModel *modelInfo;
@end
@implementation YJButtonTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setupViews];
}
return self;
}
- (void)setupViews
{
self.sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.sureButton setTitle:@"确认" forState:UIControlStateNormal];
self.sureButton.backgroundColor = [UIColor greenColor];
[self.sureButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.sureButton];
self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
self.deleteButton.backgroundColor = [UIColor redColor];
[self.deleteButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.deleteButton];
[self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.contentView.mas_centerY);
make.right.equalTo(self.contentView.mas_right).offset(-20);
}];
[self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.contentView.mas_centerY);
make.right.equalTo(self.sureButton.mas_left).offset(-20);
}];
}
//赋值操作
- (void)setModel:(YJButtonModel *)model
{
self.modelInfo = model;
self.textLabel.text = model.title;
}
//按钮点击事件
- (void)buttonAction:(UIButton *)sender
{
[sender routerWithEventName:TransferNameEvent modelInfo:@{ButtonTag:sender.titleLabel.text, ModelInfo:self.modelInfo}];
}
Controller中主要方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YJButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonCell forIndexPath:indexPath];
cell.model = self.dataArray[indexPath.row];
// Configure the cell...
return cell;
}
//操作按钮
- (void)routerWithEventName:(NSString *)eventName modelInfo:(NSDictionary *)modelInfo
{
if ([eventName isEqualToString:TransferNameEvent]) {
if ([modelInfo[ButtonTag] isEqualToString:@"确认"]) {
NSLog(@"确认:%@", [modelInfo[ModelInfo] title]);
} else {
NSLog(@"删除:%@", [modelInfo[ModelInfo] title]);
//移除model对象
[self.dataArray removeObject:modelInfo[ModelInfo]];
//刷新
[self.baseTableView reloadData];
}
}
}