ios - Swift 无法识别的选择器发送到实例错误

标签 ios swift uitableview selector

我最近将我的项目从 Objective-C 转换为 Swift,在这样做的过程中,每当我单击表格 View 单元格中的按钮时,我都会收到此错误。我有多个单元格被来自 mysql 服务器的信息填充。我有两个按钮,一个关注按钮和一个关注按钮,当点击一个时,另一个应该显示。我已经为此工作了一段时间,但一直卡在这个错误上。

当我点击表格 View 中的按钮时出现错误

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 中的按钮 selector(("")) 但错误是说 -[CustomCellSwift.ViewController followButtonClick :] 这意味着在 ViewController 中跟随按钮代码,但我不知道该怎么做。

最佳答案

Swift 3 的两个变化:

选择器应该如下所示:

#selector(ClassName.followButtonClick(_:))

函数应该有一个下划线:

@IBAction func followButtonClick(_ sender: UIButton!) { ...

请注意,这两个应该在同一个类中,否则,请确保您初始化了 ClassName 类。

如果您希望选择器方法(followButtonClick(_:)) 在UITableViewCell 类中。删除 @IBAction(我认为您在那里不需要它):

func followButtonClick(_ sender: UIButton!) { ... 

关于ios - Swift 无法识别的选择器发送到实例错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40756801/

相关文章:

ios - 你将如何在 react-native 中实现 pinch-zoom?

ios - 通过 UITableVIew 中的部分快速通用基本搜索功能

ios - 如何获取 UICollectionViewCell 的矩形?

ios - 使用node appium-xcuitest-driver测试ios应用程序

ios - 打包 iOS 应用程序后推送通知停止工作

ios - Swift - 如何更改 UITabBarItem 角标(Badge)字体?

swift - 在 ViewController 和 SwiftUI 之间共享变量

添加新实体(核心数据)时出现 Swift 错误

使用 Decodable 进行 JSON 解析

iphone - sizeWithFont - 包含 & 字符时的奇怪行为