iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell

后端 未结 10 1911
遥遥无期
遥遥无期 2021-01-01 15:45

I currently have a UITableView that is populated with a custom UITableViewCell that is in a separate nib. In the cell, there are two buttons that are wired to actions in th

相关标签:
10条回答
  • 2021-01-01 16:07
        [btnFavroite setAccessibilityValue:[NSString stringWithFormat:@"%d",indexPath.row]];
        [btnFavroite setAccessibilityLabel:btnFavroite.titleLabel.text];
        [btnFavroite addTarget:self action:@selector(btnFavClick:) forControlEvents:UIControlEventTouchUpInside];
    
    -(void)btnFavClick:(id)sender{
        UIButton *btn=(UIButton *)sender;
        int index=[btn.accessibilityValue integerValue]]
    }
    
    0 讨论(0)
  • 2021-01-01 16:09

    You could use the tag property on the button to specify which row the button was created in, if you're not using tags for anything else.

    0 讨论(0)
  • 2021-01-01 16:10

    I have the same scenario. To achieve this, I derived a custom cell. I added two properties, section and row. I also added an owner, which would be my derived TableViewController class. When the cells are being asked for, I set the section/row based on the indexPath, along with the owner.

    cell.section = indexPath.section cell.row = indexPath.row cell.owner = self

    The next thing that I did was when I created the buttons, I associate the button events with the cell rather than with the tableViewController. The event handler can read the section and row entry and send the appropriate message (or event) to the TableViewController. This greatly simplifies house keeping and maintenance by leveraging existing methods and housekeeping and keeping the cell as self contained as possible. Since the system keeps track of cells already, why do it twice!

    0 讨论(0)
  • 2021-01-01 16:13

    Much easier solution is to define your button callback with (id)sender and use that to dig out the table row index. Here's some sample code:

    - (IBAction)buttonWasPressed:(id)sender
    {
        NSIndexPath *indexPath =
            [self.myTableView
             indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
        NSUInteger row = indexPath.row;
    
        // Do something with row index
    }
    

    Most likely you used that same row index to create/fill the cell, so it should be trivial to identify what your button should now do. No need to play with tags and try to keep them in order!

    Clarification: if you currently use -(IBAction)buttonWasPressed; just redefine it as -(IBAction)buttonWasPressed:(id)sender; The additional callback argument is there, no need to do anything extra to get it. Also remember to reconnect your button to new callback in Interface Builder!

    0 讨论(0)
  • 2021-01-01 16:18

    There are multiple methods to fix the problem.

    1. You can use the "tag" property Give the value indexPath.row as the tag value for the button.
      btn.tag = indexPath.row;

    Then in the button function, you can easily access the tag value and it will be the index for the clicked button.

    -(void)btnClicked:(id)sender
    {
        int index = [sender tag];
    }
    
    1. You can use the layer property Add the indexPath as the value in the layer dictionary.

      [[btn layer] setValue:indexPath forKey:@"indexPath"];

    This indexPath is accessible from the button action function.

    -(void)btnClicked:(id)sender
    {
        NSIndexPath *indexPath = [[sender layer] valueForKey:@"indexPath"];
        int index = indexPath.row;
    }
    

    With this method you can pass multiple values to the button function just by adding new objects in the dictionary with different keys.

    0 讨论(0)
  • 2021-01-01 16:19

    Even easier:

    -(IBAction) buttonPressed {
        NSIndexPath *myIndexPath = [(UITableView *)self.superview indexPathForCell: self];
        // do whatever you need to do with the information
    }
    
    0 讨论(0)
提交回复
热议问题