如何在TableView中更改按钮的标题(Swift)

问题描述:

我试图在按下某个按钮时调出一个电话号码。 TableView中按钮的标题应该与我加如何在TableView中更改按钮的标题(Swift)

这里接触字典中改变的是按钮:

@IBAction func phoneLabel(sender: UIButton) { 

    let url:NSURL = NSURL(string: "tel://contacts.phone")! 
    UIApplication.shared.openURL(url as URL) 
} 

而这正是我试图设置按钮平铺到的实现代码如下联系电话号码

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! customCell 
     let contact = contacts.contactWithIndex((indexPath as NSIndexPath).row) 
     print(contact) 
    cell.phoneLabel.setTitle("\(phone)", for:UIControlState) 
    return cell 
} 

发生错误,表示按钮没有setTitle的值。任何帮助都是极好的!

+0

这是一个没有标签的按钮 – Mannopson

+0

,如果你看一下代码phoneLabel是按钮 –

+0

的名字你为它创建一个IBOutlet? IBAction仅用于处理按钮按钮等。 – creeperspeak

假设它是一个按钮而不是标签(查看您的发件人定义),您必须指定UIControlState.normal,而不是UIControlState。

https://developer.apple.com/reference/uikit/uicontrolstate/1618233-normal

+0

另外,您可能需要将'“\(phone)”'更改为'“\(contact.phone)”'。或者,也许你有一个叫做'phone'的全局变量,但这没有意义。 – creeperspeak

+0

也是如此,但这并不能解释“setTitle没有价值”的问题。 – nickdnk

+0

同意 - 只是认为值得指出。 – creeperspeak