2016-11-08 143 views
-1

好吧我在Swift中有一个iOS应用程序。我有一个自定义tableview,并在我的单元格中有一个按钮(2个按钮,但在修复一个,我可以修复其他)。单击按钮时,单元格应该从一个数组移动到另一个数组,但我的问题是当我单击按钮时,应用程序崩溃,无法识别的选择器。任何帮助?Swift - TableView崩溃中的按钮

错误:

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 

    } 
} 
+0

为什么你把一个按钮后删除在表格视图单元格中。你不能使用func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)对选中的表单元格作出反应,然后移动到新的数组中? – MacUserT

+0

如果添加目标,请检查您是否在pinEditor或代码中有连接到“followButtonClick”操作。 –

+0

@MacUserT我不能,那不是那个设计是怎么样的 – BroSimple

回答

-1

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

您使用此阵

1.followedArray

2.testArray

请检查元素是否正确添加和插入细胞或删除细胞

+0

在Objective-C中一切正常,但后来我转换成了Swift,现在它不起作用 – BroSimple

+0

请更正语法,以便在代码中将swift 3.0中的按钮添加到按钮 添加如下代码的动作,myFirstButton.addTarget(self,action:#selector(myClass.pressed(_ :)),forControlEvents:.TouchUpInside) – Bucket

+0

由于我是iOS新手,这段代码有什么区别? – BroSimple