一. 目的:
实现UITableViewCell上按钮点击事件可以进行页面跳转.
二. 实现方法:
1. 用协议的方式的实现.
2. 需要自定义UITableViewCell.
三. 代码部分.
cell.h中
#import <UIKit/UIKit.h> @protocol SevenProtocolDelegate <NSObject> - (void)sevenProrocolMethod:(UIViewController *)viewController and:(NSInteger)cellRow; @end @interface SevenCell : UITableViewCell @property (nonatomic, weak) id<SevenProtocolDelegate> customDelegate; @property (nonatomic, strong) UIViewController * viewController; @property (nonatomic, assign) NSInteger cellRow; @end
cell.m中
#import "SevenCell.h" #import "Masonry.h" @interface SevenCell () @property (nonatomic, strong) UIView * bgView; @property (nonatomic, strong) UIButton * button; @end @implementation SevenCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self addSubviews]; } return self; } - (void)addSubviews { [self addSubview:self.bgView]; [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 10, 0)); }]; [self.bgView addSubview:self.button]; [self.button mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.bgView).with.offset(10); make.top.mas_equalTo(self.bgView).with.offset(10); make.size.mas_equalTo(CGSizeMake(200, 40)); }]; } - (void)buttonClicked { if (self.customDelegate != nil && [self.customDelegate respondsToSelector:@selector(sevenProrocolMethod:and:)]) { [self.customDelegate sevenProrocolMethod:self.viewController and:self.cellRow]; } } #pragma mark - setter & getter - (UIView *)bgView { if (!_bgView) { self.bgView = [[UIView alloc] init]; self.bgView.backgroundColor = [UIColor whiteColor]; } return _bgView; } - (UIButton *)button { if (!_button) { self.button = [UIButton buttonWithType:UIButtonTypeCustom]; self.button.layer.masksToBounds = YES; self.button.layer.cornerRadius = 20.0f; self.button.backgroundColor = [UIColor orangeColor]; self.button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f]; [self.button setTitle:@"button点击事件跳转下一个页面" forState:UIControlStateNormal]; [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; } return _button; } @end
controller.m中
#import "SevenViewController.h" #import "SevenCell.h" #import "Seven_oneViewController.h" #import "Masonry.h" @interface SevenViewController () <UITableViewDelegate,UITableViewDataSource,SevenProtocolDelegate> @property (nonatomic, strong) UITableView * tableView; @end @implementation SevenViewController #pragma mark - 生命周期 #pragma mark viewDidLoad - (void)viewDidLoad { [super viewDidLoad]; [self basicSetting]; [self addTableView]; } #pragma mark - 系统代理 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * identifier = @"cell"; SevenCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[SevenCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.customDelegate = self; cell.viewController = self; cell.cellRow = indexPath.row; return cell; } #pragma mark - 点击事件 #pragma mark - 实现方法 #pragma mark - 自定义协议 - (void)sevenProrocolMethod:(UIViewController *)viewController and:(NSInteger)cellRow { // 可以通过 cellRow 区分哪个cell上的button跳转对应的页面 if (cellRow == 0 || cellRow == 1) { Seven_oneViewController * seven_one = [[Seven_oneViewController alloc] init]; [self.navigationController pushViewController:seven_one animated:YES]; } } #pragma mark 基本设置 - (void)basicSetting { self.title = @"cell上button页面跳转"; } - (void)addTableView { [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 0, 0, 0)); }]; } #pragma mark - setter & getter - (UITableView *)tableView { if (!_tableView) { self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; } return _tableView; } @end