一. 目的:

实现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. iphone6 plus 为什么打印出的宽度是375

    首先看一张比较简单明了的 iPhone 6 与 iPhone 6 Plus 对比图,来自 PaintCode 的<The Ultimate Guide To iPhone Resolutions ...

  2. Java 命名空间的由来和引入

    名字可视性(Name visibility) 名字管理对任何程序设计语言来说,都是一个重要问题.如果你在程序的某个模块里使用了 一个名字,而其他人在这个程序的另一个模块里也使用了相同的名字,那么怎样才 ...

  3. 用Wireshark提取WPA握手包

    进入正文前,先来看一张截图,如图1,使用“aircrack-ng wpa.cap -w password.lst”命令后,程序会提示输入待破解网络的序号,此时只要提供一个序号即可.注意:1:命令中不需 ...

  4. samba权限之easy举例说明--原创

    实验环境RHEL5.0,samba3.023rc-2 一.何为browsealbe=no? 如图lingdao目录的权限为777 如图ling目录的共享设置和用户的ID和组 当用户lingdao_01 ...

  5. windows & mac 安装lua

    mac从源码编译安装是最方便的,lua源码不足两万行,编译几秒钟的事. 打开terminal,依次输入以下命令: curl -R -O http://www.lua.org/ftp/lua-5.2.3 ...

  6. dubbo发布web服务实例

    dubbo角色与调用执行过程 dubbo节点角色说明:provider: 暴露服务的服务提供方consumer: 调用远程服务的服务消费方registry: 服务注册于发现的注册中心monitor: ...

  7. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

  8. int跟byte[]数组互转的方法,整数 + 浮点型

    整数: int转byte数组 public static byte[] intToBytes2(int n){ ]; ;i < ;i++) { b[i]=(-i*)); } return b; ...

  9. 仿网易漂亮的TAB选项卡(标签)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. VS2010安装失败解决办法

    1. 运行regedit打开注册表: 2. 找到HKEY_LOCAL_MACHINE\SOFWARE\ Microsoft\ Internet Explorer\ MAIN: 3. MAIN子键的权限 ...