自定义UITableViewCell 上的多个按钮点击事件处理
2015-12-30 21:13
513 查看
今天突然做项目的时候,又遇到处理自定义的UITableViewCell上按钮的点击事件问题。我知道有两种方式,可是突然想不起来之前是怎么做的了,好记性不如烂笔头,还是记录一下吧。
1、第一种方式给Button加上tag值
这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。
[objc] view
plaincopy
- (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;
}
然后在点击事件中取数据,加信息
[objc] view
plaincopy
- (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里添加一个代理方法。
[objc] view
plaincopy
#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
然后再控制器中实现Cell的代理方法即可
[objc] view
plaincopy
#pragma mark - TermCellDelegate
- (void)choseTerm:(UIButton *)button
{
_clickIndex = button.tag;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确定修改学期吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil 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实现的。
1、第一种方式给Button加上tag值
这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。
[objc] view
plaincopy
- (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;
}
然后在点击事件中取数据,加信息
[objc] view
plaincopy
- (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里添加一个代理方法。
[objc] view
plaincopy
#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
然后再控制器中实现Cell的代理方法即可
[objc] view
plaincopy
#pragma mark - TermCellDelegate
- (void)choseTerm:(UIButton *)button
{
_clickIndex = button.tag;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确定修改学期吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil 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实现的。
相关文章推荐
- 我认为JSP有问题(上)
- Xamarin iOS开发实战上册-----2.2.2 使用代码添加视图
- Linux下开发Windows平台运行的程序 - MinGW
- 代码实现Cell的自定制
- Heartbeat 简介
- 如何减少浏览器repaint和reflow(上)
- 安装 ssh 的公开密匙到 iPhone 上
- Pythonsocket上传和下载文件
- iOS中最简单实用的自定义动态返回行高的cell,动态计算cell的高度
- iOS中自定义输入文本框的cell(UITextFieldCell)的使用技巧
- 数据挖掘中所需的概率论与数理统计知识,上
- python 模拟post上传图片
- iOS 坐标转换,tableViewCell图片点击放大缩小回原位 —— HERO博客
- iOS 基于MVVM设计模式练习UITableView使用 —— HERO博客
- iOS 基于MVC设计模式练习UITableView使用 —— 4000 HERO博客
- 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格
- 关于 断点续传上传与秒传 你绕不过去的坎 html5+php
- NSURLConnection被弃用,请用NSURLSession(转)
- Python中的上下文管理器相关知识详解
- Python上下文管理器用法及实例解析