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实现的。
posted on 2015-05-28 09:39 CodingForever 阅读(1307) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器