iOS-UITableView-处理cell上按钮事件(弹出警示框,页面跳转等)
一. 目的:
实现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上按钮事件(弹出警示框,页面跳转等)的更多相关文章
- PHP弹出提示框并跳转到新页面即重定向到新页面
本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下 这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...
- ios兼容 input输入时弹出键盘框 页面整体上移键盘框消失后在ios上页面不能回弹的问题
前端h5混合开发手机端ios 当有input输入时,手机下方弹出键盘使页面上移,当输入完成,键盘消失后页面显示回到原位,但实际不能点击(可点击上方区域,有反应),也就是说实际是没有回弹. 解决办法: ...
- 【2017-05-21】WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性、Js中getAttribute和超链接点击弹出警示框。
一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值,方式: href="地址?key= ...
- WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性和超链接点击弹出警示框
一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值方式: href="地址?key=v ...
- 获取cell上按钮事件
原由:点击cell上的按钮,无法获取button对应的cell位置 //获取按钮上层控件,也就是cell本身 AccountCell *cell= (AccountCell *)[按钮名称 super ...
- GridView控件中插入自定义删除按钮并弹出确认框
GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...
- jquery 微信端 点击物理返回按钮,弹出提示框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML-通过点击网页上的文字弹出QQ添加好友页面
在网上参考了部分方法,综合了一下. 发现有2中方式: 第一种是不能直接弹出添加界面的,只能弹出网页,再通过网页中的添加好友才能添加: 弹出的网页是这样的(我是写成在新的网页中打开) 现在看实现的代码: ...
- iOS 获取自定义cell上按钮所对应cell的indexPath.row的方法
在UITableView或UICollectionView的自定义cell中创建一button,在点击该按钮时知道该按钮所在的cell在UITableView或UICollectionView中的行数 ...
随机推荐
- LPC18xx LPC43xx LPC4370 Bootrom USB DFU FPB - Flash Patch and Breakpoint Unit
What is the difference between a Bootrom vs bootloader on ARM systems Bootrom Bootrom (or Boot ROM) ...
- vs2013 下载
http://download.microsoft.com/download/2/4/9/249BF223-5B84-4259-9424-429E66F45509/VS2013_RC_ULT_CHS. ...
- AngularJS指令嵌套时link函数执行顺序的问题
今天研究指令嵌套时,发现子指令的link函数先于父指令的link函数执行. 这样和预想的顺序不一样. 也就是说,如果子指令的某个scope变量依赖于父指令传来的参数时,可能一直是undefinded比 ...
- 【linux】文件隐藏属性
这些隐藏的属性确实对于系统有很大的帮助的- 尤其是在系统安全 (Security) 上面,重要的紧呢!不过要先强调的是,底下的chattr指令只能在Ext2/Ext3的文件系统上面生效, 其他 ...
- Test Tex
\begin{equation}\label{exampleone}r = r_F+ \beta (r_M - r_F) + \epsilon\end{equation}
- Chapter 5. Graph Theory:: Fundamentals:: Intermediate
10457 - Magic Car 题意一开始看起来有点费解,其实最后就是要起点到终点的路径上最大边与最小边之差越小越好.这样我们可以先将边排个序,然后枚举路径上的最小边,之后依次将比它大的边按升序的 ...
- Apple individual program 加入之后的玩法 官方资源
Apple individual program 加入之后的玩法 官方资源 0. 开发资源文档 0.1 开发文档: https://developer.apple.com/support/res ...
- Linux文件和windows文件在 换行符的区别
Linux或Unix文件,和windows文件,在来回处理时,如果不注意 换行符的区别,可能导致程序错误!!!深刻的教训.... 在早期的打印机时代,开始新的一行要占用两个字符的时间.如果到了一行的结 ...
- js实现hash
由于项目中用到了hash,自己实现了一个. Hash = function () { } Hash.prototype = { constructor: Hash, add: function (k, ...
- 在Oracle Linux Server release 6.4下配置ocfs2文件系统
① 安装ocfs-tools-1.8 如果是使用RedHat Enterprise Linux 6.4,也可以安装ocfs-tools-1.8的,只是要插入Oracle Linux Server re ...