1

I dynamically created several buttons in my UITableViewCell class like so:

for (clientObjectId, _) in connectedObjectIds {
    self.clientNameButton = UIButton(type: UIButtonType.System) as UIButton
    self.clientNameButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByTruncatingTail
    self.clientNameButton.frame = CGRectMake(self.leftSideSpaceForUsersAndUserLabel, clientNameButtonFrameHeight, self.nameButtonWidth, self.nameButtonHeight)
    self.clientNameButton.setTitle(self.userObjectIdsAndNames[clientObjectId], forState: UIControlState.Normal)
    self.clientNameButton.titleLabel!.font = UIFont(name: "Helvetica", size: 12.0)
    self.clientNameButton.addTarget(self, action: "asdf:", forControlEvents: UIControlEvents.TouchUpInside)
    self.nameButtons.append(self.clientNameButton)
    self.addSubview(self.clientNameButton)
}

I want to call the following function in my UITableView class:

func asdf(sender:UIButton) {
     print("Button tapped")
}

I am thinking of using a protocal to receive the asdf() function call from my UITableView class. But is there a better way?

Edit

The difference between the possible duplicates is that the addTarget occurs in the UITableView class in a UITableView delegate function. However, I do not addTarget in the delegate function but rather in my UITableViewCell. I already read that post and it did solve my problem. That is why I asked it here with another post.

Also, the other possible duplicate was my question. And I asked this question in that post but was asking two questions in one post, so I decided to post this post so that I am not asking two questions in one post.

2

1 Answer 1

1

try this code,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell Identifier";

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.playButton.tag = indexPath.row
    cell.playButton.addTarget(self, action: Selector("buttonPressed:"), forControlEvents: .TouchUpInside)
    return cell;

}

Action:

func buttonPressed(sender:UIButton!) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! PlayViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }

hope its helpful

Sign up to request clarification or add additional context in comments.

3 Comments

I appreciate the answer but it doesn't answer my question because you add the target in the delegate function. For me, I am adding the target in my UITableViewCell class.
this way code is very simple, if you pass value this to other viewcontroller, pass the value, then its working for me
And also you will get the particular indexpath.row value in sender.tag from buttonPressed method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.