生活随笔
收集整理的這篇文章主要介紹了
iOS 自定义Cell按钮的点击代理事件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在實(shí)際開發(fā)工作中,我們經(jīng)常會(huì)在自定義的Cell中布局一些按鈕,并且很多時(shí)候我們會(huì)在點(diǎn)擊這個(gè)按鈕的時(shí)候使我們的UItableviewController跳轉(zhuǎn)到下一界面,有的可能還要傳值。那么如何使我們的控制器能夠獲知我們按下了cell的按鈕呢?毫無疑問,這是個(gè)代理模式的典型應(yīng)用場(chǎng)景。
首先我們先得定義一個(gè)cell。.h文件如下:
[objc]?view plain
?copy ? @protocol?MycellDelegate?<NSObject>?? ?? @optional?? -(void)didClickButton:(UIButton?*)button;?? ?? @end?? ?? @interface?Mycell?:?UITableViewCell?? ?? +(instancetype)cellWithtableView:(UITableView?*)tableview;?? ?? @property(nonatomic,strong)DateModel?*model;?? ?? @property(nonatomic,weak)?id<MycellDelegate>?delegate;??
.m文件如下:
[objc]?view plain
?copy ? #import?"Mycell.h"?? ?? @interface?Mycell()?? ?? @property(nonatomic,strong)UIButton?*button;?? ?? @end?? ?? @implementation?Mycell?? ?? +(instancetype)cellWithtableView:(UITableView?*)tableview?? {?? ????static?NSString?*ID?=?@"cell";?? ????Mycell?*cell?=?[tableview?dequeueReusableCellWithIdentifier:ID];?? ????if(!cell)?? ????{?? ????????cell?=?[[Mycell?alloc]initWithStyle:UITableViewCellStyleDefault?reuseIdentifier:ID];?? ????????cell.selectionStyle?=?UITableViewCellSelectionStyleNone;?? ????????cell.textLabel.font?=?[UIFont?systemFontOfSize:13.0];?? ????}?? ????return?cell;?? ?????? }?? ?? ?? ?? -(instancetype)initWithStyle:(UITableViewCellStyle)style?reuseIdentifier:(NSString?*)reuseIdentifier?? {?? ????self?=?[super?initWithStyle:style?reuseIdentifier:reuseIdentifier];?? ????if(self)?? ????{?? ????????self.button?=?[[UIButton?alloc]?initWithFrame:CGRectMake(0,?0,?[UIScreen?mainScreen].bounds.size.width,?self.frame.size.height)];?? ????????[self.button?setTitle:@"我是按鈕點(diǎn)我"?forState:UIControlStateNormal];?? ????????[self.button?setTitleColor:[UIColor?redColor]?forState:UIControlStateNormal];?? ????????self.button.contentHorizontalAlignment?=?UIControlContentHorizontalAlignmentRight;?? ????????self.button.titleLabel.font?=?[UIFont?systemFontOfSize:12.0];?? ????????[self.contentView?addSubview:self.button];?? ????????[self.button?addTarget:self?action:@selector(btnClick:)?forControlEvents:UIControlEventTouchUpInside];?? ????}?? ????return?self;?? }?? ?? ?? ?? -(void)setModel:(DateModel?*)model?? {?? ????self.textLabel.text?=?[NSString?stringWithFormat:@"日期:%@、信息:%@",model.date,model.message];?? ?????? }?? ?? #pragma?mark?-?按鈕點(diǎn)擊事件,通過代理模式響應(yīng)?? -(void)btnClick:(UIButton?*)btn?? {?? ????[self.delegate?didClickButton:btn];?? }?? ?? @end??
上述代碼定義了一個(gè)代理,當(dāng)按下按鈕時(shí),代理響應(yīng)。
現(xiàn)在回到UItableviewController,代碼如下:
[objc]?view plain
?copy ? -(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath?? {?? ????Mycell?*cell?=?[Mycell?cellWithtableView:tableView];?? ????cell.model?=?self.Array[indexPath.row];?? ????cell.delegate?=?self;?? ????return?cell;?? }??
別忘了.delegate = self哦!代理執(zhí)行的代碼如下:
[objc]?view plain
?copy ? #pragma?mark?-?代理事件?? ?? -(void)didClickButton:(UIButton?*)button?? {?? ????Mycell?*cell?=?(Mycell?*)button.superview.superview;?? ????NSIndexPath?*indexPath?=?[self.tableView?indexPathForCell:cell];?? ????MessageController?*vc?=?[[MessageController?alloc]init];?? ????DateModel?*model?=?self.Array[indexPath.row];?? ????vc.message?=?[NSString?stringWithFormat:@"行號(hào):第%ld行,日期:%@、信息:%@",(long)indexPath.row,model.date,model.message];?? ????[self.navigationController?pushViewController:vc?animated:YES];?? }??
當(dāng)我們的cell中按鈕點(diǎn)擊后,將會(huì)自動(dòng)跳到這個(gè)代理方法中,我們獲取到按鈕所在的cell,通過indexPathforCel這個(gè)方法(系統(tǒng)API)可以獲取到cell的行數(shù),并拿到數(shù)據(jù)源,此時(shí)想要傳值給下一個(gè)界面就變的非常簡(jiǎn)單。
特別提醒:
當(dāng)同一個(gè)工廠方法創(chuàng)建多行cell中的button時(shí),要分別做處理的話需要分別對(duì)應(yīng)cell的button添加tag,設(shè)置代理事件時(shí),通過tag值來區(qū)分
總結(jié)
以上是生活随笔為你收集整理的iOS 自定义Cell按钮的点击代理事件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。