【问题标题】:Pass multiple parameter using cell button action使用单元格按钮操作传递多个参数
【发布时间】:2017-02-21 14:34:49
【问题描述】:

注意:我不需要任何关于使用 UITableview 的 didselect 委托发送数据的建议

myButton.h

#import <UIKit/UIKit.h>
@interface myButton : UIButton
{
    id userData;
}

@property (strong,nonatomic) NSString* data1;

@end

myButton.m

#import "myButton.h"

@implementation myButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

我的 testCollectionViewCell.h 的自定义单元格

#import "myButton.h"
@interface testCollectionViewCell : UICollectionViewCell 
@property (nonatomic,weak) IBOutlet myButton *serviceFav;

我的 testCollectionViewCell.m 自定义单元格

#import "testCollectionViewCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation testCollectionViewCell
 @synthesize serviceFav;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        //self.serviceFav=[myButton buttonWithType:UIButtonTypeCustom];

    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{



}

@end

这是我的集合视图委托代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    testCollectionViewCell *cell = [testCollectionViewCell dequeueReusableCellWithReuseIdentifier:@"testCollectionViewCell" forIndexPath:indexPath];
    cellData = [self.collectionData objectAtIndex:[indexPath row]];

    cell.serviceFav.data1 = @"data1"; **//shows error here and kills**

    [cell.serviceFav addTarget:self action:@selector(touchUpHandler:) forControlEvents:UIControlEventTouchUpInside];


    return cell;
}

//------这不叫----我想叫它

-(void)touchUpHandler:(myButton*)sender
{

    UIButton *button = (myButton *)sender; //instance of UIButton
    NSLog(@"Data 1 = %@",sender.data1);
}

【问题讨论】:

  • 我认为你的问题有一个错字,你的意思是说fav_action没有被调用而不是touchUpHandler
  • 编辑完成,请立即查看
  • 这里是什么myButton。我假设按钮只有cell.btnLike
  • 检查附加链接代码它是 UIButton 的子类
  • 我认为您的代码没有任何问题。

标签: ios objective-c xcode uitableview uibutton


【解决方案1】:

我刚刚使用与问题中相同的过程制作了一个示例,并且成功了。

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

采用自定义按钮类

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

为您的CustomCell 类创建一个 IBOutlet

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

单击单元格中的按钮时,它会转到选择器方法touchUpHandler 并在控制台上打印

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

【讨论】:

  • @iphonemaclover 如果你附上你的完整代码并上传到保管箱,这样我可以检查更好
  • 我想看看你在哪里为UICollectionView提供数据源
猜你喜欢
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 2020-03-05
  • 2012-07-19
  • 2021-04-14
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多