1

I am creating custom button in table view and in the action of that button I want to print index path of particular row. I am calling the method by selectors.

Code:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     NSString *cellIdentifier=@"";
     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

     if(!cell)
     {

     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

      //  cell.textLabel.text=@"Hello";

    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2, cell.contentView.frame.size.height/2-10, 20, 30)];


    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-20, 0, 20, cell.contentView.frame.size.height-20)];
    [btn setBackgroundImage:[UIImage imageNamed:@"band"] forState:UIControlStateNormal];


    [ btn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside ];

    [cell.contentView addSubview:btn];

    [cell.contentView addSubview:img];
    img.image=[UIImage imageNamed:@"band"];

    }
    return cell;
    }




-(void)change:(NSIndexPath *)idd
    {
     NSLog(@"%@",idd);

    }

Problem: How can I pass parameters NSIndexPath in selectors?

Bhupesh Kumar
  • 369
  • 2
  • 18

1 Answers1

1

I believe button can carry only one input, so set the indexpath to your btn tag and use that

btn.tag=indexPath.row;
[ btn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside ];

- (void) change:(id)sender  
    {    
       NSLog(@"%d", sender.tag);  
    }
Francis F
  • 3,157
  • 3
  • 41
  • 79
  • If I call a method like this: `[var method];` Here I can pass parameters. Can't we do same using selectors.@gamerlegend – Bhupesh Kumar Aug 11 '15 at 11:35
  • 1
    you cant pass parameter to change: here. If you really want to use indexpath then use a customtableview cell as mentioned by Joe or use a category class. Have a look @ this answer http://stackoverflow.com/questions/3988485/passing-parameters-to-addtargetactionforcontrolevents – Francis F Aug 11 '15 at 11:47