Error: Index Path on a UIButton (Swift)
Error: Index Path on a UIButton (Swift)
我能够成功地将 phone 数字附加到 phoneNumbers 数组中,但是当我尝试使用 indexPath 时,我收到一条错误消息:"unrecognized selector sent to instance." 这是否意味着我不能将 indexPath 与 callButton 函数一起使用吗?如果是这样,我还能做些什么?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! TableViewCell
cell.callButton.addTarget(self, action: "callButton", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func callButton(indexPath: NSIndexPath) {
UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://\(phoneNumbers[indexPath.row])")!)
}
您的错误出现是因为在您使用 Interface Builder 设置 @IBAction
并将其注册为其目标之后,您更改了它的签名并添加了参数,这使得 unrecognized selector sent to instance
.
一旦您在自定义单元格 class 中为 UIButoon
定义了 IBOutlet
,您就可以在 class 中访问和设置任何您想要的 class =16=],像下面这样:
CustomTableViewCell
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
var phoneNumber: Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func callButton(sender: AnyObject) {
println("Calling to \(self.phoneNumber)")
}
}
然后在您的 UITableViewController
或 UIViewController
中,您可以像下面的代码一样为 UIButton
设置您想要的内容:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
// Set the phone number according the indexPath.row of the cell to call later
cell.phoneNumber = self.phoneNumberList[indexPath.row]
return cell
}
当您在 cellForRowAtIndexPath
中设置 phoneNumber
时,您为每个单元格设置了不同的 phoneNumber
,这是在 cellForRowAtIndexPath
中执行此操作的重点。
希望对你有所帮助。
我能够成功地将 phone 数字附加到 phoneNumbers 数组中,但是当我尝试使用 indexPath 时,我收到一条错误消息:"unrecognized selector sent to instance." 这是否意味着我不能将 indexPath 与 callButton 函数一起使用吗?如果是这样,我还能做些什么?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! TableViewCell
cell.callButton.addTarget(self, action: "callButton", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func callButton(indexPath: NSIndexPath) {
UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://\(phoneNumbers[indexPath.row])")!)
}
您的错误出现是因为在您使用 Interface Builder 设置 @IBAction
并将其注册为其目标之后,您更改了它的签名并添加了参数,这使得 unrecognized selector sent to instance
.
一旦您在自定义单元格 class 中为 UIButoon
定义了 IBOutlet
,您就可以在 class 中访问和设置任何您想要的 class =16=],像下面这样:
CustomTableViewCell
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
var phoneNumber: Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func callButton(sender: AnyObject) {
println("Calling to \(self.phoneNumber)")
}
}
然后在您的 UITableViewController
或 UIViewController
中,您可以像下面的代码一样为 UIButton
设置您想要的内容:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
// Set the phone number according the indexPath.row of the cell to call later
cell.phoneNumber = self.phoneNumberList[indexPath.row]
return cell
}
当您在 cellForRowAtIndexPath
中设置 phoneNumber
时,您为每个单元格设置了不同的 phoneNumber
,这是在 cellForRowAtIndexPath
中执行此操作的重点。
希望对你有所帮助。