【原】自定义tableviewcell中多个button点击实现不同功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #import <UIKit/UIKit.h> @protocol TableCellDelegate < NSObject > - ( void )choseTerm:(UIButton *)button; @end @interface TableViewCell : UITableViewCell @property (weak, nonatomic ) IBOutlet UIButton *checkButton; @property (weak, nonatomic ) IBOutlet UIButton *checkButton2; @property (weak, nonatomic ) IBOutlet UILabel *label; @property (assign, nonatomic ) BOOL isChecked; @property (assign, nonatomic ) id <TableCellDelegate> delegate; - ( IBAction )checkAction:(UIButton *)sender; - ( IBAction )checkAction2:(UIButton *)sender; |
.m文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #import "TableViewCell.h" @implementation TableViewCell - ( void )awakeFromNib { // Initialization code } - ( void )setSelected:( BOOL )selected animated:( BOOL )animated { [ super setSelected:selected animated:animated]; // Configure the view for the selected state } - ( IBAction )checkAction:(UIButton *)sender { if ([_delegate respondsToSelector: @selector (choseTerm:)]) { sender.tag = self .checkButton.tag; NSLog (@ "tag:%ld" , self .checkButton.tag); [_delegate choseTerm:sender]; } } - ( IBAction )checkAction2:(UIButton *)sender { if ([_delegate respondsToSelector: @selector (choseTerm:)]) { sender.tag = self .checkButton2.tag; NSLog (@ "tag2:%ld" , self .checkButton2.tag); [_delegate choseTerm:sender]; } } @end |
viewcontroller.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #import "ViewController.h" #import "TableViewCell.h" @interface ViewController ()<UITableViewDelegate, UITableViewDelegate, TableCellDelegate> @end @implementation ViewController - ( void )viewDidLoad { [ super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - ( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section { return 5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath { static NSString *cellIdentifier = @ "Cell" ; static BOOL nibsRegistered = NO ; if (!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@ "TableViewCell" bundle: nil ]; [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; nibsRegistered = YES ; } TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; cell.delegate = self ; cell.checkButton.tag = indexPath.row*1000+1; cell.checkButton2.tag = indexPath.row*1000+2; NSLog (@ "checkButton.tag:%ld;checkButton2.tag=%ld" ,cell.checkButton.tag,cell.checkButton2.tag); cell.label.text = [ NSString stringWithFormat:@ "%ld" ,( long )indexPath.row]; return cell; } - ( void )choseTerm:(UIButton *)button { _clickIndex = button.tag; if (_clickIndex%1000 == 1) { NSLog (@ "第一列!" ); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@ "确定修改学期吗?" message: nil delegate: self cancelButtonTitle:@ "取消" otherButtonTitles:@ "确定" , nil , nil ]; [alertView show]; } else { NSLog (@ "第二列!" ); } } @end |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步