2016-11-08 51 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