自定义UITableViewCell 上按钮点击事件处理
2015-07-22 10:09
661 查看
1、第一种方式给Button加上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
然后再控制器中实现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里面跳转,这种耦合性比较强。思路先是找到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;
}
头文件就不写了,很简单的扩展。
[objc] view
plaincopy
- (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];
};
}
- (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
然后再控制器中实现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里面跳转,这种耦合性比较强。思路先是找到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;
}
头文件就不写了,很简单的扩展。
[objc] view
plaincopy
- (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];
};
}
相关文章推荐
- 【UIKit】-2- UIAccelerometer-加速计事件
- HDU 5288 OO’s Sequence(多校第一场1001)
- 关于mule中使用jdbc时报No Suitable Driver found错误的问题
- Convert UOM values
- No suitable driver found for jdbc:mysql://localhost:3306/test
- 【UIKit】-1- UIKitDefines.h - 跨平台,包容 C 库
- UISearchBar套接TableView实现搜索功能
- 禁用UITextField的双击出现复制粘贴剪切等操作
- 【Android UI设计与开发】第15期:顶部标题栏(六)实现悬浮式顶部和底部标题栏效果
- IOS中UIview需要知道的一些事情:setNeedsDisplay、setNeedsLayout
- queue的用法以及优先级priority的用法
- hdu contest day1 1001 OO’s Sequence
- IOS 开发之UITableview的基本创建
- OO’s Sequence 2015多校联合1001
- iOS总结控件二(UIButton)
- rabbitmq之back queue草稿
- rabbitmq method之queue.declare
- EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(四)
- LeetCode N-Queens
- LeetCode N-Queens