Iphone 使用objc_setAssociatedObject为UITableViewCell传递UIButton@selector参数

Iphone 使用objc_setAssociatedObject为UITableViewCell传递UIButton@selector参数,iphone,objective-c,uitableview,uibutton,Iphone,Objective C,Uitableview,Uibutton,我试图在这篇文章中实现一个建议:使用objc\u setAssociatedObject和objc\u getAssociatedObject将@selector参数传递给UITableViewCell中的UIButton。按照我的编码方式,它总是传递它创建/加载的最后一个单元格的行。这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexP

我试图在这篇文章中实现一个建议:使用
objc\u setAssociatedObject
objc\u getAssociatedObject
@selector
参数传递给
UITableViewCell
中的
UIButton
。按照我的编码方式,它总是传递它创建/加载的最后一个单元格的行。这是我的密码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *mainLabel;
    soundButton=[UIButton buttonWithType:UIButtonTypeCustom];

    if (cell == nil){

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    soundButton.tag = 33;
        [soundButton addTarget:self action:@selector(soundButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [soundButton setFrame:CGRectMake(210,3,68, 37)];

         [soundButton setBackgroundImage:[UIImage imageNamed:@"musicNote"] forState:UIControlStateNormal];

         [cell.contentView addSubview:soundButton];
    } else {

        soundButton = (UIButton *)[cell.contentView viewWithTag:33];
    }
    objc_setAssociatedObject(soundButton, "IndexPath", indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return cell;
}

-(void)soundButtonAction
{
    NSIndexPath *ip = objc_getAssociatedObject(soundButton, "IndexPath");

看起来
soundButton
是你们班的ivar吗?每次请求单元格时,它都是get override,因此您只能获取最后一个单元格

另外,我认为使用
“indepath”
作为键不是一个好主意

static char indexPathKey; // use address of indexPathKey as key

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *mainLabel;
    soundButton=[UIButton buttonWithType:UIButtonTypeCustom];

    if (cell == nil){

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    soundButton.tag = 33;
        [soundButton addTarget:self action:@selector(soundButtonAction:) /*extra :*/ forControlEvents:UIControlEventTouchUpInside];
        [soundButton setFrame:CGRectMake(210,3,68, 37)];

         [soundButton setBackgroundImage:[UIImage imageNamed:@"musicNote"] forState:UIControlStateNormal];

         [cell.contentView addSubview:soundButton];
    } else {

        soundButton = (UIButton *)[cell.contentView viewWithTag:33];
    }
    objc_setAssociatedObject(soundButton, &indexPathKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return cell;
}

-(void)soundButtonAction:(UIButton *)sender
{
    NSIndexPath *ip = objc_getAssociatedObject(sender, &indexPathKey);

看起来
soundButton
是你们班的ivar吗?每次请求单元格时,它都是get override,因此您只能获取最后一个单元格

另外,我认为使用
“indepath”
作为键不是一个好主意

static char indexPathKey; // use address of indexPathKey as key

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *mainLabel;
    soundButton=[UIButton buttonWithType:UIButtonTypeCustom];

    if (cell == nil){

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    soundButton.tag = 33;
        [soundButton addTarget:self action:@selector(soundButtonAction:) /*extra :*/ forControlEvents:UIControlEventTouchUpInside];
        [soundButton setFrame:CGRectMake(210,3,68, 37)];

         [soundButton setBackgroundImage:[UIImage imageNamed:@"musicNote"] forState:UIControlStateNormal];

         [cell.contentView addSubview:soundButton];
    } else {

        soundButton = (UIButton *)[cell.contentView viewWithTag:33];
    }
    objc_setAssociatedObject(soundButton, &indexPathKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return cell;
}

-(void)soundButtonAction:(UIButton *)sender
{
    NSIndexPath *ip = objc_getAssociatedObject(sender, &indexPathKey);