2016-12-14 40 views
0

夫特3夫特:斷言故障時按鈕點擊

我有幾個行的表圖,每一行有一個按鈕(使用indexPath.row)時,按鈕被點擊時,它從第1部分移動所述行( testArray)至部分0(,其次是數組)。但是當使用搜索欄搜索testArray並單擊某行的按鈕時,它會崩潰。

現在,使用搜索欄時,testArray被過濾到一個新的數組稱爲filteredArray。該按鈕應該將該行從filteredArray移動到後跟陣列

沒有搜索,按鈕按預期工作,它只在使用搜索欄時崩潰。

我在做什麼錯?

這裏是ERROR我得到:

CustomCellSwift[1473:391841] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.5.2/UITableView.m:1315 

新建議:filteredArray取代時莫非有什麼與具有testArray的indexPath.row和followedArray衝突它表視圖?

Deleting from UISearchController's filtered search results

我正在使用的代碼:

@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) { 

     // Change Follow to Following 
     (sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal) 
     cell.followButton.isHidden = true 
     cell.followedButton.isHidden = false 

     // Checking wether to import from testArray or filteredArray to followedArray 
     if !(searchController.isActive && searchController.searchBar.text != "") { 

      self.myTableView.beginUpdates() 

      // ----- Inserting Cell to followedArray ----- 
      followedArray.insert(testArray[indexPath.row], at: 0) 
      myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade) 

      // ----- Removing Cell from testArray ----- 
      testArray.remove(at: indexPath.row) 
      let rowToRemove = indexPath.row 
      self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade) 

      self.myTableView.endUpdates() 

     } 
     else { 

      self.myTableView.beginUpdates() 

      // ----- Inserting Cell to followedArray ----- 
      followedArray.insert(filteredArray[indexPath.row], at: 0) 
      myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade) 

      // ----- Removing Cell from filteredArray ----- 
      filteredArray.remove(at: indexPath.row) 
      let rowToRemove = indexPath.row 
      self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade) 

      self.myTableView.endUpdates() 

     } 
    } 
} 

回答

0

所以我找到答案,我用插入在區段方法,而不是將所述對象到所述陣列使用只需插入,因爲我只有1節。

let testObject: Test = filteredArray[indexPath.row] 
let indexOfObjectInArray = testArray.index(of: testObject) 
followedArray.insert(testObject, at: 0)