【问题标题】:Swift: 'attempt to delete row 0 from section 0 which only contains 0 rows before the update'Swift:'尝试从第 0 节中删除第 0 行,更新前仅包含 0 行'
【发布时间】:2017-07-07 00:43:04
【问题描述】:

为什么会出现此错误?我需要做什么?

* -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.8.1/UITableView.m:1442 2017-07-06 20:25:30.736267-0400 BlogApp[1482:340583] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试从之前仅包含 0 行的第 0 节中删除第 0 行更新”

崩溃在这里

// ----- Inserting Cell to followedArray -----
let blogObject: Blog = filteredArray[indexPath.section][indexPath.row]
let indexOfObjectInArray = mainArray.index(of: blogObject)

followedArray.insert(blogObject, at: 0)

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

self.myTableView.endUpdates()

我从未使用过数组数组var filteredArray = [[Blog]](),所以也许我只是没有正确访问或删除它。

我刚刚发布了有关解决我遇到的 SearchBar 问题的帖子,但在我尝试它时,我遇到了这个崩溃。当我在搜索对象时单击关注按钮时会发生这种情况。这超出了我的理解,因为我是 SearchBar 代码的新手。

从filteredArray中删除单元格时出现问题,可能没有正确访问它所以无法删除它?我已经逐行设置了断点,当从filteredArray中删除单元格时它崩溃了

另外,我在使用 SearchBar 时遇到了一个一般性问题并获得了新代码,所以也许这可以提供帮助。

Swift: Have SearchBar search through both sections and not combine them

如有其他信息,请告诉我,谢谢。

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

        // 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 mainArray or filteredArray to followedArray
        if !(searchController.isActive && searchController.searchBar.text != "") {

            self.myTableView.beginUpdates()

            // Save identifier into followedIdentifier array
            self.followedIdentifiers.insert(mainArray[indexPath.row].blogID)

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

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

            self.myTableView.endUpdates()

            // After Updating Table, Save the Archived Data to File Manager
            saveData()
        }
        else { // **** Crash in this section ****

            self.myTableView.beginUpdates()

            // Remove identifier into followedIdentifier array
            self.followedIdentifiers.remove(followedArray[indexPath.row].blogID)

            // ----- Inserting Cell to followedArray -----
            let blogObject: Blog = filteredArray[indexPath.section][indexPath.row]
            let indexOfObjectInArray = mainArray.index(of: blogObject)

            followedArray.insert(blogObject, at: 0)

            //------------------------
            // CRASH SHOULD BE HERE (breakpoints lead me here)
            //------------------------

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

            self.myTableView.endUpdates()

            // After Updating Table, Save the Archived Data to File Manager
            saveData()
        }
    }
}

我的其余代码,可能有助于解决问题

var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [[Blog]]()

// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {
            return followedArray.count
        } else {
            return mainArray.count
        }
    } else {
        return filteredArray[section].count
    }
}

// CellForRowAt indexPath
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let CellIdentifier = "Cell"
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell

    if cell != cell {
        cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
    }

    // Configuring the cell
    var blogObject: Blog

    if !(searchController.isActive && searchController.searchBar.text != "") {
        if indexPath.section == 0 {
            blogObject = followedArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
        } else {
            blogObject = mainArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
        }
    } else {
        blogObject = filteredArray[indexPath.section][indexPath.row]
        cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
    }

    return cell
}

取消关注按钮代码

与关注按钮相反的问题应该在这里。

// Unfollow Button
@IBAction func followedButtonClick(_ 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 Following to Follow
        (sender as UIButton).setImage(UIImage(named: "followed.png")!, for: .normal)
        cell.followButton.isHidden = false
        cell.followedButton.isHidden = true

        self.myTableView.beginUpdates()

        // Remove identifier into followedIdentifier array
        self.followedIdentifiers.remove(followedArray[indexPath.row].blogID)

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

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

        self.myTableView.endUpdates()

        // After Updating Table, Save the Archived Data to File Manager
        saveData()
    }
}

【问题讨论】:

  • 可能不相关,但在insert/delete/move 操作之后永远不会调用reloadDatainsert/delete/move 处理 UI 更新,您将丢失动画。
  • reloadData 最好放在哪里?另外,我对这篇文章的措辞是否正确,希望人们能理解?
  • 删除 all reloadData() 紧跟在insertRows/deleteRows/moveRows 之后的行
  • 再次 insert/delete/move 处理 UI 更新
  • 是的,Apple 非常擅长这些东西:方法按照名称所说的方法执行。 deleteRows 将删除行。因此,在您调用它之后,行将被删除。你已经说出了你需要说的一切;您已经告诉 table view 它需要做的一切。为什么还要多说?

标签: ios swift uitableview uisearchbar


【解决方案1】:

你必须先从 TableView 中删除单元格!

// ----- Removing Cell from filteredArray -----
self.myTableView.deleteRows(at: [indexPath], with: .fade)
filteredArray.remove(at: [indexPath.section][indexPath.row])
mainArray.remove(at: indexOfObjectInArray!)

试试看。

【讨论】:

    【解决方案2】:

    您可以尝试使用不同的操作更新 tableview。

    喜欢:

    - 执行阵列更新过程 -

    - 对于插入过程-

    self.myTableView.beginUpdates()
    myTableView.insertRows(at: [IndexPath(row: 0, section: 1)], with: .fade)
    self.myTableView.endUpdates()
    

    然后

    - 执行阵列更新过程 -

    - 用于删除过程-

    self.myTableView.beginUpdates()
    self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)
    self.myTableView.endUpdates()
    

    希望它会有所帮助,因为我遇到了同样的问题,这个解决方案对我有用!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      相关资源
      最近更新 更多