【问题标题】:Swift - Button in TableView Crashing [duplicate]Swift - TableView 中的按钮崩溃 [重复]
【发布时间】:2016-11-08 07:06:50
【问题描述】:

好的,我在 Swift 中有一个 iOS 应用程序。我有一个自定义表格视图,在我的单元格中有一个按钮(2 个按钮,但在修复一个时,我可以修复另一个)。单击按钮时,单元格应该从一个数组移动到另一个数组,但我的问题是当我单击按钮时应用程序崩溃说无法识别的选择器。有什么帮助吗?

错误:

CustomCellSwift[4834:1676038] -[CustomCellSwift.ViewController followButtonClick:]: unrecognized selector sent to instance 0x100b0b490

关注按钮代码:

// Follow Button
@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()

    }
}

填充单元格代码

func populateCell(_ testObject: Test, isFollowed: Bool, indexPath: IndexPath, parentView: Any) {

    // Loading Background Color
    self.backgroundColor = UIColor.white

    // Loading Images -SDWebImage- *Crashes*
    //let imgURL = (testObject.value(forKey: "testURL") as! String) // as! NSURL
    //self.myImageView.contentMode = .scaleAspectFit
    //self.myImageView.sd_setImage(with: imgURL as URL, placeholderImage: UIImage(named: "no-image.png")) // Missing RefreshCached

    // 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

    }
}

【问题讨论】:

  • 为什么要在表格视图单元格中放置一个按钮。难道你不能使用 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 对被选中的表格单元格做出反应,然后移动到新数组?
  • 检查你是否在 pinEditor 或代码中连接到 action followButtonClick ,如果你添加了目标。
  • @MacUserT 我不能,设计不是这样

标签: ios swift uitableview compiler-errors uibutton


【解决方案1】:

确保节号和行号正确,如果在数组中找不到位置,它也会崩溃。

你正在使用这个数组

1.followedArray

2.testArray

插入单元格或删除单元格后请检查元素是否正确添加和删除

【讨论】:

  • 在 Objective-C 中一切正常,但后来我转换为 Swift,现在它不起作用
  • 请更正语法以在您的代码中为 swift 3.0 中的按钮添加操作添加操作,如以下代码 myFirstButton.addTarget(self, action: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside)
  • 由于我是iOS新手,这段代码有什么区别?
  • 在 swift 语言中,您必须使用这种方式添加按钮操作#selector(您的派系名称)
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2020-12-25
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多