简体   繁体   中英

Storyboard custom UITableviewcell selection

In my project I have custom UITableview cells, which happen to have a button in it. When this button is selected, it triggers a segue. In this segue I pass an object from the cell to the destination UIViewcontroller . I'm currently using the following code

else if ([[segue identifier] isEqualToString:@"Add"])
{
    SearchAddRecordViewController *addRecordViewController = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    SearchCell *cell = [self.tableView cellForRowAtIndexPath:path];
    NSLog(@"%@", cell.record);
    addRecordViewController.workingRecord = cell.record;
}

It turns out I'm passing null, because the button isn't triggering the selection of the cell, therefore no indexPathForSelectedRow. My question is, how do I get the indexpath for the cell which button has been pressed.

Edit: Answered

You declare an NSIndexpath variable in your custom cell's .h file and assign the indexPath to that variable in cellForRowAtindexPath. And Retrieve it and use it....Hope it will work

For anyone experiencing a similar problem, this is how I was able to solve this.

I first created the implementation and header files for a UIButton subclass with the NSIndexPath property. I assigned the uibutton in the Storyboard the superclass of this new custom unbutton with the NSIndexPath property. I added the following code to the segue identification step (the key was realizing i could use the sender object as the source of triggering the segue):

else if ([[segue identifier] isEqualToString:@"Add"])
{
    SearchAddRecordViewController *addRecordViewController = [segue destinationViewController];
    addRecordButton *button = (addRecordButton*) sender;
    NSIndexPath *path = button.indexPath;
    SearchCell *cell = [self.tableView cellForRowAtIndexPath:path];
    addRecordViewController.workingRecord = cell.record;
}

An alternative way to determine the indexPath that avoids subclassing UIButton would be:

CGPoint point = [button.superview convertPoint:button.center toView:self.tableView];
NSIndexPath *path = [self.tableView indexPathForRowAtPoint:point];

EDIT:

I've factored this into a nice method you can add to a Category on UITableView:

-(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
    if([component isDescendantOfView:self] && component != self) {
        CGPoint point = [component.superview convertPoint:component.center toView:self];
        return [self indexPathForRowAtPoint:point];
    }
    else {
        return nil;
    }
}

You can do this in following manner, this helps you to uniquely identify which button is tapped and you can pass the appropriate values, I am just passing a string value for example

suppose OutputString is a string property in your destination view controller

if ([[segue identifier] isEqualToString:@"yourSegueIdentifier"]) {

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [self.myTableView indexPathForCell:clickedCell];
    [segue.destinationViewController setOutputString:@"XYZ"];        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM