UITableViewCell上的按钮点击事件处理
转自: http://www.aichengxu.com/view/42871
UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下。
今天突然做项目的时候,又遇到处理自定义的UITableViewCell上按钮的点击事件问题。我知道有两种方式,可是突然想不起来之前是怎么做的了,好记性不如烂笔头,还是记录一下吧。
1、第一种方式给Button加上tag值
这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *identifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
User *user = _users[indexPath.row];
cell.user = user;
//拍照button
UIButton *photographButton = [UIButton buttonWithType:UIButtonTypeCustom];
photographButton.frame = CGRectMake(221 , 10, 100, 44);
[photographButton setImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
[photographButton addTarget:self action:@selector(photographButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
photographButton.tag = indexPath.row;
[cell.contentView addSubview:photographButton]; return cell;
}
然后在点击事件中取数据,加信息
- (void)photographButtonClicked:(UIButton *)sender{
User *user = _users[sender.tag];
PhotoPickerController *photoPicker = [[PhotoPickerController alloc] init];
photoPicker.user = user;
[self.navigationController pushViewController:photoPicker animated:YES];
}
以上两个方法都是在同一个控制器中。
另外一种,自定义了UITableViewCell,那么就在UITableViewCell里添加一个代理方法。
#import <UIKit/UIKit.h> @protocol TermCellDelegate <NSObject> - (void)choseTerm:(UIButton *)button; @end @interface TermCell : UITableViewCell @property (retain, nonatomic) IBOutlet UIButton *checkButton;
@property (retain, nonatomic) IBOutlet UILabel *termLabel; @property (assign, nonatomic) BOOL isChecked;
@property (assign, nonatomic) id<TermCellDelegate> delegate; - (IBAction)checkAction:(UIButton *)sender; @end #import "TermCell.h" @implementation TermCell - (void)awakeFromNib
{
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} - (void)layoutSubviews
{
[super layoutSubviews];
if (_isChecked) {
[_checkButton setBackgroundImage:[UIImage imageNamed:@"task_state_checked"] forState:UIControlStateNormal];
} else {
[_checkButton setBackgroundImage:[UIImage imageNamed:@"task_state_unchecked"] forState:UIControlStateNormal];
}
} - (void)dealloc {
[_checkButton release];
[_termLabel release];
[super dealloc];
} - (IBAction)checkAction:(UIButton *)sender {
if ([_delegate respondsToSelector:@selector(choseTerm:)]) {
sender.tag = self.tag;
[_delegate choseTerm:sender];
}
} @end
#pragma mark - TermCellDelegate
- (void)choseTerm:(UIButton *)button
{
_clickIndex = button.tag;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确定修改学期吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
当然,这里也可以做界面跳转,取数据依然用button的tag值。
第二种,是直接在自定义的Cell里面跳转,这种耦合性比较强。思路先是找到button的父控制器,然后做界面跳转或者其他操作。有这样一个工具方法
#import "UIView+Additions.h" @implementation UIView (Additions) - (UIViewController *)viewController
{
UIResponder *next = [self nextResponder];
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
} next = [next nextResponder]; } while (next != nil); return nil;
}
头文件就不写了,很简单的扩展。
- (void)setWeiboModel:(WeiboModel *)weiboModel
{
if (_weiboModel != weiboModel) {
[_weiboModel release];
_weiboModel = [weiboModel retain];
} __block WeiboCell *this = self;
_userImage.touchBlock = ^{
NSString *nickName = this.weiboModel.user.screen_name;
UserViewController *userCtrl = [[UserViewController alloc] init];
userCtrl.userName = nickName;
[this.viewController.navigationController pushViewController:userCtrl animated:YES];
[userCtrl release];
}; }
这里是给Cell赋值model,然后点击事件是用Block实现的。
UITableViewCell上的按钮点击事件处理的更多相关文章
- UITableViewCell 上的按钮点击事件
以前做tableViewCell的button点击事件,总是建立一个全局的可变数组,把data放在数组里,点击获取button的tag值,根据tag从数组了里取data. 其实,如果section只 ...
- Javascript(jQuery)中绑定页面上所有按钮点击事件的几种方式
方法一:使用document对象查找所有的按钮 [javascript] view plain copy 在CODE上查看代码片派生到我的代码片 //按照dom的方式添加事件处理 function B ...
- UItableViewCell上的button点击无响应的办法
由于IOS7中添加了滑动后出现编辑按钮的操作,所以使用scrollView来处理,UITableViewCellScrollView有对触摸的相应处理,导致按钮的点击效果被屏蔽了,但是点击事件还是在的 ...
- iOS中按钮点击事件处理方式
写在前面 在iOS开发中,时常会用到按钮,通过按钮的点击来完成界面的跳转等功能.按钮事件的实现方式有多种,其中 较为常用的是目标-动作对模式.但这种方式使得view与controller之间的耦合程度 ...
- cell上的按钮点击和左滑冲突
cell上的某个按钮的点击事件,当cell左滑的时候,只要活动的区域也在按钮上,那么按钮的点击事件也会调用. fix: 给按钮添加一个手势(TapGesture)那么当点击的时候就会响应点击手势的方法 ...
- Android学习笔记(1):常用按钮点击事件处理方式
1.从布局文件获取对应的控件然后对其添加点击监听器. Button loginBtn; @Override protected void onCreate(Bundle savedInstanceSt ...
- 【php+uploadify3.2】上传按钮点击一点反应都没有,原因
原因: 代码没有问题,这个原因也困扰我一段时间,是由于浏览器禁用了flash,需要放开,操作方法如下: 在谷歌浏览器输入:chrome://settings/content/flash 然后添加需要该 ...
- 解决WebUploader 上传按钮按F12 才行的问题
遇到了 WebUploader 插件的上传按钮点击无效(此时鼠标在按钮任何位置时,按钮都没变化).按F12 之后才有反应(此时鼠标在按钮任何位置时,按钮颜色都会变深) 的问题,网上查到一些答案,找到了 ...
- 探究 | 如何捕获一个Activity页面上所有的点击行为
前言 最近逛wanAndroid论坛,发现一个有趣的问题:如何捕获一个Activity页面上所有的点击行为. 一起研究下吧,不想看源码的小伙伴可以直接看文末总结- 准备工作 先得罗列出页面上的一些点击 ...
随机推荐
- mysql存储过程中字符串参数单引号
注意:存储过程中单引号 ,四个单引号 SET @sql = CONCAT('select user_id into ',m_user_id,' from go_user where mobile = ...
- java url中文参数乱码问题
http://www.blogjava.net/jerry-zhaoj/archive/2009/07/16/286993.html 转 JAVA 中URL链接中文参数乱码的处理方法JAVA 中URL ...
- 1021 玛丽卡 - Wikioi
题目描述 Description麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知 ...
- 把工程部署在tomcat的root路径下
myeclipse可以右键工程:(eclipse也可以)选择properties->myeclipse->web:把web context-root改成:/然后在用myeclipse部署项 ...
- Hibernate各种主键生成策略与配置详解【附1--<generator class="foreign">】
1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据库都无关,可以跨数据库.在存储对象前,必须要使用主 ...
- oracle 条件:1=1或1=0,动态添加条件
看到where语句中有条件:where 1=1 和 1=2或1<>1 用途: 1=1:是为了添加条件时使用and并列其他条件时使用的(动态连接后续条件) 比如: ...
- 01-05-01-2【Nhibernate (版本3.3.1.4000) 出入江湖】立即加载实现--NHibernateUtil.Initialize()和添加fetch关键字的HQL查询
相关资料: http://www.cnblogs.com/lyj/archive/2008/10/29/1322373.html 问题的提出: 1.延迟加载,可能会引起session已经关闭的异常,例 ...
- Kafka的消息格式
Commit Log Kafka储存消息的文件被它叫做log,按照Kafka文档的说法是: Each partition is an ordered, immutable sequence of me ...
- jquery 父页面 子页面 同级页面 调用
项目中用到 代码 ,先 展示 ,以便以后用到,直接看下就会明白. var li_divs = parent.$("#servicelist")[0].contentWindow. ...
- hdu 2736 Surprising Strings(类似哈希,字符串处理)
重点在判重的方法,嘻嘻 题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int ...