IOS UITableViewCell Detailed Explanation and Button Click Event Handling Example

  • 2021-08-21 21:39:43
  • OfStack

IOS UITableViewCell Detailed Explanation and Button Click Event Handling

Today, when I was suddenly working on the project, I encountered the problem of handling the button click event on the custom UITableViewCell. I know there are two ways, but suddenly I can't remember how to do it before. Good memory is not as good as bad writing, so record it.

1. In the first way, add tag value to Button

There are two types here: one is to directly add UIButton button to the native UITableViewCell, then set tag value to UIButton, and then take data and make interface jump in the method in the controller. Let's give an example, save half a day of memories.


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

Then take data and add information in the click event


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

The above two methods are both in the same controller.

2. If you customize UITableViewCell, add a proxy method to 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 

Then the proxy method of Cell can be implemented in the controller


#pragma mark - TermCellDelegate 
- (void)choseTerm:(UIButton *)button 
{ 
  _clickIndex = button.tag; 
  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@" Are you sure you want to revise the term? " message:nil delegate:self cancelButtonTitle:@" Cancel " otherButtonTitles:@" Determine ", nil nil]; 
  [alertView show]; 
} 

Of course, you can also do interface jump here, and the tag value of button is still used for taking data.

Add: Here can also be in the proxy method will be cell itself back, so do not take data from the array, directly use cell data object, more simple.

3. It jumps directly in the custom Cell, which has strong coupling. The idea is to find the parent controller of button first, and then do interface jump or other operations. There is such a tool method


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

The header file is not written, which is a simple extension.


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

Here, model is assigned to Cell, and then the click event is implemented with Block.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: