自定义UITableViewCell 上的多个按钮点击事件处理

今天突然做项目的时候,又遇到处理自定义的UITableViewCell上按钮的点击事件问题。我知道有两种方式,可是突然想不起来之前是怎么做的了,好记性不如烂笔头,还是记录一下吧。

1、第一种方式给Button加上tag值

这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.       
  4.     static NSString *identifier = @"Cell";  
  5.       
  6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];  
  7.     if (cell == nil) {  
  8.          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];  
  9.         cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  10.     }  
  11.      User *user = _users[indexPath.row];  
  12.     cell.user = user;  
  13.     //拍照button  
  14.     UIButton  *photographButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  15.     photographButton.frame = CGRectMake(221 , 1010044);  
  16.     [photographButton setImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];  
  17.     [photographButton addTarget:self action:@selector(photographButtonClicked:) forControlEvents:UIControlEventTouchUpInside];  
  18.     photographButton.tag = indexPath.row;  
  19.     [cell.contentView addSubview:photographButton];  
  20.       
  21.     return cell;  
  22. }  



然后在点击事件中取数据,加信息

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)photographButtonClicked:(UIButton *)sender{  
  2.      User *user = _users[sender.tag];  
  3.     PhotoPickerController *photoPicker = [[PhotoPickerController alloc] init];  
  4.     photoPicker.user = user;  
  5.     [self.navigationController pushViewController:photoPicker animated:YES];  
  6.       
  7. }  
以上两个方法都是在同一个控制器中。

另外一种,自定义了UITableViewCell,那么就在UITableViewCell里添加一个代理方法。

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @protocol TermCellDelegate <NSObject>  
  4.   
  5. - (void)choseTerm:(UIButton *)button;  
  6.   
  7. @end  
  8.   
  9. @interface TermCell : UITableViewCell  
  10.   
  11. @property (retainnonatomic) IBOutlet UIButton *checkButton;  
  12. @property (retainnonatomic) IBOutlet UILabel *termLabel;  
  13.   
  14. @property (assign, nonatomicBOOL  isChecked;  
  15. @property (assign, nonatomicid<TermCellDelegate> delegate;  
  16.   
  17. - (IBAction)checkAction:(UIButton *)sender;  
  18.   
  19. @end  
  20.   
  21. #import "TermCell.h"  
  22.   
  23. @implementation TermCell  
  24.   
  25. - (void)awakeFromNib  
  26. {  
  27.     // Initialization code  
  28. }  
  29.   
  30. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
  31. {  
  32.     [super setSelected:selected animated:animated];  
  33.   
  34.     // Configure the view for the selected state  
  35. }  
  36.   
  37. - (void)layoutSubviews  
  38. {  
  39.     [super layoutSubviews];  
  40.     if (_isChecked) {  
  41.         [_checkButton setBackgroundImage:[UIImage imageNamed:@"task_state_checked"] forState:UIControlStateNormal];  
  42.     } else {  
  43.         [_checkButton setBackgroundImage:[UIImage imageNamed:@"task_state_unchecked"] forState:UIControlStateNormal];  
  44.     }  
  45. }  
  46.   
  47. - (void)dealloc {  
  48.     [_checkButton release];  
  49.     [_termLabel release];  
  50.     [super dealloc];  
  51. }  
  52.   
  53. - (IBAction)checkAction:(UIButton *)sender {  
  54.     if ([_delegate respondsToSelector:@selector(choseTerm:)]) {  
  55.         sender.tag = self.tag;  
  56.         [_delegate choseTerm:sender];  
  57.     }  
  58. }  
  59.   
  60. @end  
然后再控制器中实现Cell的代理方法即可

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #pragma mark - TermCellDelegate  
  2. - (void)choseTerm:(UIButton *)button  
  3. {  
  4.     _clickIndex = button.tag;  
  5.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确定修改学期吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  
  6.     [alertView show];  
  7. }  
当然,这里也可以做界面跳转,取数据依然用button的tag值。

第二种,是直接在自定义的Cell里面跳转,这种耦合性比较强。思路先是找到button的父控制器,然后做界面跳转或者其他操作。有这样一个工具方法


  1. #import "UIView+Additions.h"  
  2.   
  3. @implementation UIView (Additions)  
  4.   
  5. - (UIViewController *)viewController  
  6. {  
  7.     UIResponder *next = [self nextResponder];  
  8.     do {  
  9.         if ([next isKindOfClass:[UIViewController class]]) {  
  10.             return (UIViewController *)next;  
  11.         }  
  12.           
  13.         next = [next nextResponder];  
  14.           
  15.     } while (next != nil);  
  16.       
  17.       
  18.     return nil;  
  19. }  
头文件就不写了,很简单的扩展。


  1. - (void)setWeiboModel:(WeiboModel *)weiboModel  
  2. {  
  3.     if (_weiboModel != weiboModel) {  
  4.         [_weiboModel release];  
  5.         _weiboModel = [weiboModel retain];  
  6.     }  
  7.       
  8.     __block WeiboCell *this = self;  
  9.     _userImage.touchBlock = ^{  
  10.         NSString *nickName = this.weiboModel.user.screen_name;  
  11.         UserViewController *userCtrl = [[UserViewController alloc] init];  
  12.         userCtrl.userName = nickName;  
  13.         [this.viewController.navigationController pushViewController:userCtrl animated:YES];  
  14.         [userCtrl release];  
  15.     };  
  16.       
  17. }  


这里是给Cell赋值model,然后点击事件是用Block实现的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值