从 UITableViewCell 调用 UIButton 操作 [英] Calling a UIButton action from a UITableViewCell

查看:28
本文介绍了从 UITableViewCell 调用 UIButton 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableviewCell 类中动态创建了几个按钮,如下所示:

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)
}

但是,我似乎找不到在我的表格视图单元格类中调用以下函数的方法:

However, I can't seem to find a way to call the following function in my table view cell class:

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

我不太关心能否在 UITableViewCell 类中调用它,就像在表视图类中调用函数一样.

I don't really care so much about being able to call it in my UITableViewCell class as much as my calling the function in my table view class.

有人能解决这个问题吗?

Anybody have a solution to this problem?

推荐答案

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)
}

这篇关于从 UITableViewCell 调用 UIButton 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆