一. 目的:

实现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(, , , ));
}]; [self.bgView addSubview:self.button];
[self.button mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.bgView).with.offset();
make.top.mas_equalTo(self.bgView).with.offset();
make.size.mas_equalTo(CGSizeMake(, ));
}];
} - (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 ;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} - (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 == || cellRow == )
{
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(, , , ));
}];
} #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

iOS-UITableView-处理cell上按钮事件(弹出警示框,页面跳转等)的更多相关文章

  1. PHP弹出提示框并跳转到新页面即重定向到新页面

    本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下   这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...

  2. ios兼容 input输入时弹出键盘框 页面整体上移键盘框消失后在ios上页面不能回弹的问题

    前端h5混合开发手机端ios  当有input输入时,手机下方弹出键盘使页面上移,当输入完成,键盘消失后页面显示回到原位,但实际不能点击(可点击上方区域,有反应),也就是说实际是没有回弹. 解决办法: ...

  3. 【2017-05-21】WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性、Js中getAttribute和超链接点击弹出警示框。

    一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值,方式: href="地址?key= ...

  4. WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性和超链接点击弹出警示框

    一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值方式: href="地址?key=v ...

  5. 获取cell上按钮事件

    原由:点击cell上的按钮,无法获取button对应的cell位置 //获取按钮上层控件,也就是cell本身 AccountCell *cell= (AccountCell *)[按钮名称 super ...

  6. GridView控件中插入自定义删除按钮并弹出确认框

    GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...

  7. jquery 微信端 点击物理返回按钮,弹出提示框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. HTML-通过点击网页上的文字弹出QQ添加好友页面

    在网上参考了部分方法,综合了一下. 发现有2中方式: 第一种是不能直接弹出添加界面的,只能弹出网页,再通过网页中的添加好友才能添加: 弹出的网页是这样的(我是写成在新的网页中打开) 现在看实现的代码: ...

  9. iOS 获取自定义cell上按钮所对应cell的indexPath.row的方法

    在UITableView或UICollectionView的自定义cell中创建一button,在点击该按钮时知道该按钮所在的cell在UITableView或UICollectionView中的行数 ...

随机推荐

  1. Facebook is Hiring!

    I am a software engineer in Facebook. I joined Facebook a year ago and now doing some iOS stuff. If ...

  2. Swift入门篇-字符串和字符

    今天主要是介绍一下字符串的用法 ,字符串的语法和object-c语法不太一样,但是思想是一样,就是写法不太一样.如果您对.net和java语法比较熟悉的话,那您几乎没有深压力.如果您对swift 基本 ...

  3. GEMR: Get the parent window for view

    Window window = Utilities.GetVisualParent<Window>(this); if (window != null) { window.DialogRe ...

  4. Linux--Ubuntu中文文件夹转英文

    前言 在安装Ubuntu的时候,如果选择的系统语言为汉语,安装完成后,Ubuntu系统的主文件夹下的几个文件目录就是中文的,这样才纯终端下,输入起来确实非常的不方便.当然,如果安装Ubuntu的时候, ...

  5. mysql 日期对比,varchar类型装换为datetime类型

    select * from tb_gps WHERE str_to_date(intime,'%Y-%m-%d %H:%i:%s') BETWEEN '2013-9-2 14:40:33' and ' ...

  6. Flipping elements with WPF

    http://yichuanshen.de/blog/2010/11/13/flipping-elements-with-wpf/ Have you already seen ForgottenTim ...

  7. ubuntu下取代ping的好工具tcpping

    $ sudo apt-get install tcptraceroute bc$ cd /usr/bin$ sudo wget http://www.vdberg.org/~richard/tcppi ...

  8. ORACLE 10g下载地址

    ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 密码是这个 一般不会动了 大家也不用帮我找回密码了 每次都改 也很麻烦的如果有需要可以帮我浏览下 右侧的需要 ...

  9. Rxlifecycle(三):坑

    坑1 Observable.just("hello world!") .compose(this.<String>bindUntilEvent(ActivityEven ...

  10. 系统UINavigationController使用相关参考

    闲来无事便在网上google&baidu了一番UINavigationController的相关文章,然后又看了下官方文档:看看更新到iOS7之后UINavigationController的 ...