2

This subject is all over the stackoverflow but I couldn't find a fitting solution.

I have button on each UITableViewCell.

Here is how I create the cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExaminationCell"];
    UIButton *clipButton = (UIButton *)[cell viewWithTag:3];

    MedicalExamination *examination = [objects objectAtIndex:indexPath.row];

    [clipButton addTarget:self action:@selector(examinatonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

Here is the method thah should handle the button click:

-(void)examinatonClicked:(MedicalExamination*)examination
{

}

How do I pass examination object to examinatonCommentsClicked method? Obviously the object is different for each cell...

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • You can use the solution provided in this answer regarding UIButton and passing mulitple parameters to its selector: https://stackoverflow.com/a/53779104/5324541 – Lloyd Keijzer Dec 14 '18 at 11:58

2 Answers2

3

A somewhat hacky thing that I'll do, is I'll use the UIButton's tag attribute to pass the row number so I can pull the object from the array that's backing my table.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExaminationCell"];
    UIButton *clipButton = (UIButton *)[cell viewWithTag:3];
    clipButton.tag = indexPath.row //Set the UIButton's tag to the row.

    MedicalExamination *examination = [objects objectAtIndex:indexPath.row];

    [clipButton addTarget:self action:@selector(examinatonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

-(void)examinatonClicked: (id)sender
{
     int row = ((UIButton *)sender).tag;
     MedicalExamination *examination = [objects objectAtIndex: row];
     //Profit!
}
Tonithy
  • 2,300
  • 1
  • 20
  • 20
-1

I think you have to add button in uitableviewCell through xib(custum cell) and then connect it through iboutlet.

xav
  • 5,452
  • 7
  • 48
  • 57
Rajpal Thakur
  • 355
  • 2
  • 15