Swift - TableView Crashing中的按钮

时间:2016-11-08 07:06:50

标签: ios swift uitableview compiler-errors uibutton

好的,我在Swift中有一个iOS应用程序。我有一个自定义的tableview,在我的单元格中我有一个按钮(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

    }
}

1 个答案:

答案 0 :(得分:-1)

确保部分编号和行号正确,如果未在阵列中找到位置,它也会崩溃。

您正在使用此阵列

1.followedArray

2.testArray

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