m
今天突然做项目的时候,又遇到处理自定义的UITableViewCell上按钮的点击事件问题。我知道有两种方式,可是突然想不起来之前是怎么做的了,好记性不如烂笔头,还是记录一下吧。
1、第一种方式给Button加上tag值
这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。
[objc]view plaincopy
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*identifier=@"Cell";
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:reuseIdentifier];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
User*user=_users[indexPath.row];
cell.user=user;
//拍照button
UIButton*photographButton=[UIButtonbuttonWithType:UIButtonTypeCustom];
photographButton.frame=CGRectMake(221,10,100,44);
[photographButtonsetImage:[UIImageimageNamed:@"camera.png"]forState:UIControlStateNormal];
[photographButtonaddTarget:selfaction:@selector(photographButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
photographButton.tag=indexPath.row;
[cell.contentViewaddSubview:photographButton];
returncell;
}
然后在点击事件中取数据,加信息
[objc]view plaincopy
-(void)photographButtonClicked:(UIButton*)sender{
User*user=_users[sender.tag];
PhotoPickerController*photoPicker=[[PhotoPickerControlleralloc]init];
photoPicker.user=user;
[self.navigationControllerpushViewController:photoPickeranimated:YES];
}
以上两个方法都是在同一个控制器中。另外一种,自定义了UITableViewCell,那么就在UITableViewCell里添加一个代理方法。
[objc]view plaincopy
#import<UIKit/UIKit.h>
@protocolTermCellDelegate<NSObject>
-(void)choseTerm:(UIButton*)button;
@end
@interfaceTermCell:UITableViewCell
@property(retain,nonatomic)IBOutletUIButton*checkButton;
@property(retain,nonatomic)IBOutletUILabel*termLabel;
@property(assign,nonatomic)BOOLisChecked;
@property(assign,nonatomic)id<TermCellDelegate>delegate;
-(IBAction)checkAction:(UIButton*)sender;
@end
#import"TermCell.h"
@implementationTermCell
-(void)awakeFromNib
{
//Initializationcode
}
-(void)setSelected:(BOOL)selectedanimated:(BOOL)animated
{
[supersetSelected:selectedanimated:animated];
//Configuretheviewfortheselectedstate
}
-(void)layoutSubviews
{
[superlayoutSubviews];
if(_isChecked){
[_checkButtonsetBackgroundImage:[UIImageimageNamed:@"task_state_checked"]forState:UIControlStateNormal];
}else{
[_checkButtonsetBackgroundImage:[UIImageimageNamed:@"task_state_unchecked"]forState:UIControlStateNormal];
}
}
-(void)dealloc{
[_checkButtonrelease];
[_termLabelrelease];
[superdealloc];
}
-(IBAction)checkAction:(UIButton*)sender{
if([_delegaterespondsToSelector:@selector(choseTerm:)]){
sender.tag=self.tag;
[_delegatechoseTerm:sender];
}
}
@end
然后再控制器中实现Cell的代理方法即可
[objc]view plaincopy
#pragmamark-TermCellDelegate
-(void)choseTerm:(UIButton*)button
{
_clickIndex=button.tag;
UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"确定修改学期吗?"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nilnil];
[alertViewshow];
}
当然,这里也可以做界面跳转,取数据依然用button的tag值。第二种,是直接在自定义的Cell里面跳转,这种耦合性比较强。思路先是找到button的父控制器,然后做界面跳转或者其他操作。有这样一个工具方法
[objc]view plaincopy
#import"UIView+Additions.h"
@implementationUIView(Additions)
-(UIViewController*)viewController
{
UIResponder*next=[selfnextResponder];
do{
if([nextisKindOfClass:[UIViewControllerclass]]){
return(UIViewController*)next;
}
next=[nextnextResponder];
}while(next!=nil);
returnnil;
}
头文件就不写了,很简单的扩展。
[objc]view plaincopy
-(void)setWeiboModel:(WeiboModel*)weiboModel
{
if(_weiboModel!=weiboModel){
[_weiboModelrelease];
_weiboModel=[weiboModelretain];
}
__blockWeiboCell*this=self;
_userImage.touchBlock=^{
NSString*nickName=this.weiboModel.user.screen_name;
UserViewController*userCtrl=[[UserViewControlleralloc]init];
userCtrl.userName=nickName;
[this.viewController.navigationControllerpushViewController:userCtrlanimated:YES];
[userCtrlrelease];
};
}
这里是给Cell赋值model,然后点击事件是用Block实现的。