国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例

2019-10-21 18:49:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理

今天突然做項(xiàng)目的時(shí)候,又遇到處理自定義的UITableViewCell上按鈕的點(diǎn)擊事件問(wèn)題。我知道有兩種方式,可是突然想不起來(lái)之前是怎么做的了,好記性不如爛筆頭,還是記錄一下吧。

1、第一種方式給Button加上tag值

這里分為兩種:一種是直接在原生的UITableViewCell上添加UIButton按鈕,然后給UIButton設(shè)置tag值,然后在控制器里的方法里通過(guò)取數(shù)據(jù),做界面跳轉(zhuǎn)等。還是舉個(gè)例子吧,省的回憶半天。

- (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; } 

然后在點(diǎn)擊事件中取數(shù)據(jù),加信息

- (void)photographButtonClicked:(UIButton *)sender{    User *user = _users[sender.tag];   PhotoPickerController *photoPicker = [[PhotoPickerController alloc] init];   photoPicker.user = user;   [self.navigationController pushViewController:photoPicker animated:YES];    } 

以上兩個(gè)方法都是在同一個(gè)控制器中。

2、自定義了UITableViewCell,那么就在UITableViewCell里添加一個(gè)代理方法。

#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 

然后再控制器中實(shí)現(xiàn)Cell的代理方法即可

#pragma mark - TermCellDelegate - (void)choseTerm:(UIButton *)button {   _clickIndex = button.tag;   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"確定修改學(xué)期嗎?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil nil];   [alertView show]; } 

當(dāng)然,這里也可以做界面跳轉(zhuǎn),取數(shù)據(jù)依然用button的tag值。

補(bǔ)充:這里還可以在代理方法中將cell本身傳回去,這樣不用從數(shù)組取數(shù)據(jù),直接利用cell的數(shù)據(jù)對(duì)象,更簡(jiǎn)單吆。

3、是直接在自定義的Cell里面跳轉(zhuǎn),這種耦合性比較強(qiáng)。思路先是找到button的父控制器,然后做界面跳轉(zhuǎn)或者其他操作。有這樣一個(gè)工具方法

#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; } 

頭文件就不寫(xiě)了,很簡(jiǎn)單的擴(kuò)展。

- (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,然后點(diǎn)擊事件是用Block實(shí)現(xiàn)的。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 利辛县| 莱阳市| 南宫市| 丰顺县| 本溪| 怀集县| 镇雄县| 峨眉山市| 仁化县| 白朗县| 时尚| 临泽县| 金川县| 丹凤县| 文成县| 山阳县| 昂仁县| 榆中县| 东乡族自治县| 凤山市| 吉水县| 兴安盟| 正阳县| 县级市| 香格里拉县| 修水县| 临西县| 穆棱市| 八宿县| 观塘区| 衡阳县| 扬中市| 通化市| 鱼台县| 珲春市| 虹口区| 屏边| 白银市| 朝阳市| 扎赉特旗| 大埔区|