IOS UITableViewCell详解及按钮点击事件处理实例

所属分类: 软件编程 / IOS 阅读数: 807
收藏 0 赞 0 分享

IOS 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]; 
   
} 

以上两个方法都是在同一个控制器中。

2、自定义了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 

然后再控制器中实现Cell的代理方法即可

#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本身传回去,这样不用从数组取数据,直接利用cell的数据对象,更简单吆。

3、是直接在自定义的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实现的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

iOS开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多