亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

IOS UITableViewCell詳解及按鈕點擊事件處理實例

 更新時間:2016年12月21日 09:11:40   投稿:lqh  
這篇文章主要介紹了IOS UITableViewCell詳解及按鈕點擊事件處理實例的相關(guān)資料,這里附有示例代碼,大家可以看下如何實現(xiàn)按鍵點擊事件,需要的朋友可以參考下

IOS UITableViewCell詳解及按鈕點擊事件處理

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

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

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

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

然后在點擊事件中取數(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]; 
   
} 

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

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 

然后再控制器中實現(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]; 
} 

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

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

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

#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實現(xiàn)的。

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

相關(guān)文章

  • iOS中多線程的入門使用教程(Swift)

    iOS中多線程的入門使用教程(Swift)

    這篇文章主要給大家介紹了關(guān)于iOS中多線程入門使用的相關(guān)資料,一個進程中可以開啟多條線程,每條線程可以并行執(zhí)行不同的任務(wù),本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • IOS中各種手勢操作實例代碼

    IOS中各種手勢操作實例代碼

    IOS中手勢操作一般是 UIGestureRecognizer 類的幾個手勢子類去實現(xiàn),一般我們用到的手勢就這么5種,具體哪幾種大家通過本文學(xué)習(xí)吧,本文重點給大家介紹IOS中各種手勢操作實例代碼,一起看看吧
    2017-03-03
  • UITableView中Cell重用機制導(dǎo)致內(nèi)容重復(fù)的解決方法

    UITableView中Cell重用機制導(dǎo)致內(nèi)容重復(fù)的解決方法

    這篇文章主要為大家詳細介紹了UITableView中Cell重用機制導(dǎo)致內(nèi)容重復(fù)的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • ios使用OC寫算法之遞歸實現(xiàn)八皇后

    ios使用OC寫算法之遞歸實現(xiàn)八皇后

    本篇文章主要介紹了ios使用OC寫算法之遞歸實現(xiàn)八皇后,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 詳解iOS的Core Animation框架中的CATransform3D圖形變換

    詳解iOS的Core Animation框架中的CATransform3D圖形變換

    CATransform3D一般用于操作view的layer的,是Core Animation的結(jié)構(gòu)體,可以用來做比較復(fù)雜的3D操作,這里我們就帶大家來詳解iOS的Core Animation框架中的CATransform3D圖形變換
    2016-07-07
  • iOS百度地圖簡單使用詳解

    iOS百度地圖簡單使用詳解

    百度地圖的功能有很多,本篇文章主要介紹了iOS百度地圖簡單使用詳解,具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • IOS客戶端接入微信支付

    IOS客戶端接入微信支付

    對于一個ios的app,如果有一些虛擬的商品或者服務(wù)需要通過在線支付來收費的話,一般有幾種主流的選擇。如果是通過APP調(diào)用支付平臺APP的思路的話,一個是調(diào)起支付寶客戶端,一個則是調(diào)起微信支付。本文給大家分享ios客戶端接入微信支付,需要的朋友可以參考下
    2015-09-09
  • IOS 常見內(nèi)存泄漏以及解決方案

    IOS 常見內(nèi)存泄漏以及解決方案

    這篇文章主要介紹了IOS 常見內(nèi)存泄漏以及解決方案的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求詳解

    iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求詳解

    這篇文章主要給大家介紹了關(guān)于iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS Xcode自定義代碼塊及遷移的實現(xiàn)方法

    iOS Xcode自定義代碼塊及遷移的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于iOS Xcode自定義代碼塊及遷移的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用iOS Xcode具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論