IOS UITableViewCell詳解及按鈕點擊事件處理執行個體_IOS

來源:互聯網
上載者:User

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實現的。

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.