我最近将我的项目从Objective-C转换为Swift,这样每当我点击表格视图单元格中的按钮时,我就会收到此错误。我有多个单元格充满了来自mysql服务器的信息。我有两个按钮,一个按钮和一个按钮,当一个单击时,另一个应该显示。我已经在这方面工作了一段时间,但我一直坚持这个错误。
我在点击表格视图中的按钮时收到错误
CustomCellSwift[1425:372289] -[CustomCellSwift.ViewController followButtonClick:]: unrecognized selector sent to instance 0x100b13a40
在CustomCell.swift
中class CustomCell: UITableViewCell {
@IBOutlet weak var firstStatusLabel: UILabel!
@IBOutlet weak var secondStatusLabel: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var followButton: UIButton!
@IBOutlet weak var followedButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.followButton.isHidden = true
self.followedButton.isHidden = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func populateCell(_ testObject: Test, isFollowed: Bool, indexPath: IndexPath, parentView: Any) {
// Loading Background Color
self.backgroundColor = UIColor.white
// Loading Status Labels
self.firstStatusLabel.text = testObject.testStatus1
self.secondStatusLabel.text = testObject.testStatus2
self.firstStatusLabel.isHidden = true
self.secondStatusLabel.isHidden = true
if isFollowed {
self.followedButton.tag = indexPath.row
self.followedButton.addTarget(parentView, action: Selector(("followedButtonClick")), for: .touchUpInside)
self.followedButton.isHidden = false
self.followButton.isHidden = true
// Status Labels
self.firstStatusLabel.isHidden = false
self.secondStatusLabel.isHidden = false
}
else {
self.followButton.tag = indexPath.row
self.followButton.addTarget(parentView, action: Selector(("followButtonClick:")), for: .touchUpInside)
self.followedButton.isHidden = true
self.followButton.isHidden = false
// Status Labels
self.firstStatusLabel.isHidden = false // True when done testing
self.secondStatusLabel.isHidden = false // True when done testing
}
}
}
ViewController.swift
CellForRowAt indexPath
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
if cell != cell {
cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
}
// Coloring TableView
myTableView.backgroundColor = UIColor.white
// Configuring the cell
var testObject: Test
if !isFiltered {
if indexPath.section == 0 {
testObject = followedArray[indexPath.row]
cell.populateCell(testObject, isFollowed: true, indexPath: indexPath, parentView: self)
}
else if indexPath.section == 1 {
testObject = testArray[indexPath.row]
cell.populateCell(testObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
}
else {
testObject = filteredArray[indexPath.row] as! Test
cell.populateCell(testObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
return cell
}
按照按钮代码
@IBAction func followButtonClick(sender: UIButton!) {
// Adding row to tag
let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
// Showing Status Labels
let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
cell.firstStatusLabel.isHidden = false
cell.secondStatusLabel.isHidden = false
// Change Follow to Following
(sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
cell.followButton.isHidden = true
cell.followedButton.isHidden = false
self.myTableView.beginUpdates()
// ----- Inserting Cell to Section 0 -----
followedArray.insert(testArray[indexPath.row], at: 0)
myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
// ----- Removing Cell from Section 1 -----
testArray.remove(at: indexPath.row)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
self.myTableView.endUpdates()
}
}
取消关注按钮代码与关注按钮相同。
我认为问题出现在按钮CustomCell.swift
的{{1}}中,但错误是selector((""))
,这意味着在跟随按钮代码的ViewController中,但我不知道是什么再做一次。
答案 0 :(得分:4)
Swift 3 的两项更改:
选择器应如下所示:
#selector(ClassName.followButtonClick(_:))
该函数应该有一个下划线:
@IBAction func followButtonClick(_ sender: UIButton!) { ...
请注意,这两个应该在同一个类中,否则,请确保初始化ClassName
类。
如果您希望选择器方法(followButtonClick(_:)
)位于UITableViewCell
类中。删除@IBAction
(我认为你不需要它):
func followButtonClick(_ sender: UIButton!) { ...
答案 1 :(得分:0)
Swift 2.2
与Xcode 8
:
self.followedButton.addTarget(parentView, action: #selector(CustomCell.followButtonClick(_:)), forControlEvents: .TouchUpInside)
答案 2 :(得分:0)
对于Swift3
,您需要更改以下内容:
self.followedButton.addTarget(parentView, action: Selector(("followedButtonClick")), for: .touchUpInside)
使用:
self.followedButton.addTarget(parentView, action: #selector(self.followButtonClick(_:)), forControlEvents: .touchUpInside)