Pass multiple parameter using cell button action

后端 未结 1 1122
小鲜肉
小鲜肉 2021-01-26 02:44

NOTE: i don\'t need any suggestion regarding sending data using didselect delegate of UITableview

myButton.h

#import &l         


        
相关标签:
1条回答
  • 2021-01-26 03:43

    I just made a sample with the same process as in the Question and it worked.

    Link: https://www.dropbox.com/s/lb2k7nzsytxjnl2/tabletest.zip?dl=0

    Took a custom Button Class

    MyButton.h

    #import <UIKit/UIKit.h>
    
    @interface MyButton : UIButton
    
    @property (strong,nonatomic) NSString* data1;
    @property (strong,nonatomic) NSString* data2;
    
    @end
    

    MyButton.m

    #import "MyButton.h"
    
    @implementation MyButton
    
    @end
    

    Make an IBOutlet to your CustomCell Class

    CustomTableViewCell.h

    #import <UIKit/UIKit.h>
    #import "MyButton.h"
    
    @interface CustomTableViewCell : UITableViewCell
    @property (weak, nonatomic) IBOutlet MyButton *btnLike;
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    #import "CustomTableViewCell.h"
    
    @interface ViewController ()
    
    @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 {
    
        CustomTableViewCell* cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
    
        cell.btnLike.data1 = @"data1";
        cell.btnLike.data2 = @"data2";
    
        [cell.btnLike addTarget:self action:@selector(touchUpHandler:) forControlEvents:UIControlEventTouchUpInside];
    
        return cell;
    
    }
    
    - (void) touchUpHandler:(MyButton *)sender {
    
        NSLog(@"Data 1 = %@",sender.data1);
        NSLog(@"Data 2 = %@",sender.data2);
    
    }
    @end
    

    On clicking on the button in the cell, it goes to the selector method touchUpHandler and prints on console

    2016-10-12 23:14:39.034 tabletest[28414:671128] Data 1 = data1
    2016-10-12 23:14:39.035 tabletest[28414:671128] Data 2 = data2
    
    0 讨论(0)
提交回复
热议问题