【问题标题】:Swift: Assertion failure when button clickedSwift:单击按钮时断言失败
【发布时间】:2016-12-14 03:57:52
【问题描述】:

斯威夫特 3

我有一个包含几行的表格视图,每一行都有一个按钮(使用 indexPath.row),当单击该按钮时,它会将行从第 1 节 (testArray) 移动到第 0 节( followedArray)。但是当使用搜索栏搜索 testArray 并单击一行按钮时,它会崩溃。

现在在使用搜索栏时,testArray 被过滤成一个名为 filteredArray 的新数组。该按钮应该将行从 filteredArray 移动到 followedArray

不搜索,按钮按预期工作,仅在使用搜索栏时崩溃。

我做错了什么?

这是我得到的错误

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

新建议:是不是和filteredArray替换table view时testArray和followedArray的indexPath.row冲突有关系?

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()

        }
    }
}

【问题讨论】:

    标签: ios swift uitableview crash uisearchcontroller


    【解决方案1】:

    所以我找到了答案,我使用 insert at section 方法将对象添加到数组中,而不是只使用 insert,因为我只有 1 个部分。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2018-03-12
      • 2011-05-27
      相关资源
      最近更新 更多