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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
aejhyun
  • 612
  • 1
  • 6
  • 19
  • Possible duplicate of [iOS Swift Button action in table view cell](http://stackoverflow.com/questions/28894765/ios-swift-button-action-in-table-view-cell) – Patrick May 03 '16 at 11:23
  • Possible duplicate of http://stackoverflow.com/questions/37002308/calling-a-uibutton-action-from-a-uitableviewcell – Hasya May 03 '16 at 11:24

1 Answers1

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

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • 1
    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. – aejhyun May 03 '16 at 11:34
  • this way code is very simple, if you pass value this to other viewcontroller, pass the value, then its working for me – Iyyappan Ravi May 03 '16 at 11:40
  • 1
    And also you will get the particular `indexpath.row` value in `sender.tag` from `buttonPressed` method – Iyyappan Ravi May 03 '16 at 11:42