Get UITableViewCell of UIButton?

I’m trying to perform a segue using a UIButton, which is located within in a custom UITableViewCell class called GFHomeCell.

The GFHomeCell has a postID property, which I want to send in the prepare for segue. I set up a method to run when the button is pressed; however, in the button-pressed method, I need the sender to be a GFHomeCell(or at least that’s what I assume).

  • How to correctly turn on and off SKEffectNode for a SKNode?
  • Swift 3 UIView animation
  • Invalid Bundle Structure - iOS
  • Xcode 9, Carthage. iTunes Connect Error: “Invalid Bundle - Disallowed LLVM instrumentation”
  • What is diffrence between self.timer = nil vs in iOS?
  • Touch event handled by multiple views
  • Does anyone have any ideas how I can do that? Here is my code

    My cellForRowAtIndexPath:

            GFHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsfeedCell" forIndexPath:indexPath];
    
            NSDictionary *rootObject = self.posts[indexPath.row];
            NSDictionary *post = rootObject[@"post"];
            NSDictionary *group = post[@"group"];
    
            NSString *groupName = group[@"name"];
    
            cell.actionLabel.text = [NSString stringWithFormat:@"New post trending on %@", groupName];
            cell.descriptionLabel.text = post[@"body"];
            cell.descriptionLabel.numberOfLines = 0;
            cell.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
            cell.likesLabel.text = [NSString stringWithFormat:@"%@", post[@"likes"]];
            cell.postId = post[@"id"];
            cell.groupName = group[@"name"];
            cell.postBody = post[@"body"];
            cell.likeButton.tag = indexPath.row;
            [cell.likeButton addTarget:self action:@selector(likeButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
            [cell.commentButton addTarget:self action:@selector(commentButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
    
            NSString *urlString = [NSString stringWithFormat:@"%s/%@", kBaseURL, @"images/"];
            NSURL *url = [NSURL URLWithString:urlString];
            [cell.imageView setImageWithURL:url
                           placeholderImage:[UIImage imageNamed:@"Newsfeed-Image-Placeholder"]];
    
            return cell;
    

    Here is the method I’m running when the button is clicked. My thought was that I need the sender here to be a cell, not a button, as the postId property I’m sending in my prepareForSegue only exists on a GFHomeCell:

    - (void)commentButtonClick:(id)sender {
        [self performSegueWithIdentifier:@"addCommentSegue" sender:sender];
    }
    

    Finally my prepareForSegue(I only included the part relevant to this segue):

    } else if ([segue.identifier isEqualToString:@"addCommentSegue"]) {
       GFPostShowViewController *destViewController = segue.destinationViewController;
        GFHomeCell * cell = sender;
    
        destViewController.postId = [cell.postId copy];
        destViewController.groupName = [cell.groupName copy];
        destViewController.postBody = [cell.postBody copy];
    
    } else {}
    

    I’m new to iOS and this has me stumped so any help would be much appreciated, thanks.

    2 Solutions Collect From Internet About “Get UITableViewCell of UIButton?”

    There are basically two common approaches to this situation. One is to search up through the button’s superviews until you find the cell. You shouldn’t rely on going up one or two levels, because the hierarchy has changed in the past, and may change again (you need to go up two levels in iOS 6, but 3 in iOS 7). You can do it like this,

    -(void)commentButtonClick:(UIButton *) sender {
        id superView = sender.superview;
        while (superView && ![superView isKindOfClass:[UITableViewCell class]]) {
            superView = [superView superview];
        }
        [self performSegueWithIdentifier:@"addCommentSegue" sender:superView];
    }
    

    The other way is to assign a tag to your button in cellForRowAtIndexPath: equal to the indexPath.row (if you only have one section), and then use sender.tag to get the indexPath of the cell that contained the tapped button.

    Well, one answer would be to just go up a level in the view hierarchy:

     - (void)commentButtonClick:(id)sender {
        GFHomeCell * cell = (GFHomeCell *) [(UIButton*)sender superview];
        if (cell && [cell Class] == [GFHomeCell class]) {
            //do whatever with cell.postID
            [self performSegueWithIdentifier:@"addCommentSegue" sender:sender];
         }
    }
    

    Oh, I forget… you may have to go up two levels to get past the contentView property:

       GFHomeCell * cell = (GFHomeCell *) [[(UIButton*)sender superview] superview];