ios - 向 UITableView 添加行时更新无效

标签 ios objective-c xcode uitableview swift

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

我正在尝试在用户点击一行时将行添加到 TableView ,以创建一个可扩展部分,但是在 Xcode 尝试添加它们之前不会计算额外的行,因此会导致此错误(我认为)。谁能指出我正确的方向?

// sectionExpanded is set to false in viewDidLoad. It is set to true when
// the user taps on the expandable section (section 0 in this case)

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 && sectionExpanded {
        return 5
    } else {
        return 1
    }
}

// This should recount the rows, add the new ones to a temporary array and then add
// them to the table causing the section to 'expand'.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedItem = menu[indexPath.row]
    let cell = tableView.cellForRowAtIndexPath(indexPath) as MenuCell
    if indexPath.section == 0 {
        var rows: Int
        var tmpArray: NSMutableArray = NSMutableArray()

        sectionExpanded = !sectionExpanded
        rows = tableView.numberOfRowsInSection(0)

        for i in 1...rows {
            var tmpIndexPath: NSIndexPath
            tmpIndexPath = NSIndexPath(forRow: i, inSection: 0)

            tmpArray.addObject(tmpIndexPath)
        }

        if !sectionExpanded {
            tableView.deleteRowsAtIndexPaths(tmpArray, withRowAnimation: UITableViewRowAnimation.Top)
        } else {
            tableView.insertRowsAtIndexPaths(tmpArray, withRowAnimation: UITableViewRowAnimation.Top)
        }
    } else {
        delegate?.rightItemSelected(selectedItem)
    }
}

最佳答案

它告诉您您正在尝试插入 1 个新行,但是 numberofrows 应该是 5,之前是 1,而您正在尝试插入 1 个新行,那是 2。这就是您的问题。

rows = tableView.numberOfRowsInSection(0) //this returns 1
for i in 1...rows { //
    var tmpIndexPath: NSIndexPath
    tmpIndexPath = NSIndexPath(forRow: i, inSection: 0)

    tmpArray.addObject(tmpIndexPath)//this will contain only 1 object, because the loop will run only for 1 cycle
}

编辑

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedItem = menu[indexPath.row]
    let cell = tableView.cellForRowAtIndexPath(indexPath) as MenuCell
    if indexPath.section == 0 {
        var rows: Int
        var tmpArray: NSMutableArray = NSMutableArray()

        sectionExpanded = !sectionExpanded
        rows = 1
        if sectionExpanded {
            rows = 5
        }

        for i in 1...rows {
            var tmpIndexPath: NSIndexPath
            tmpIndexPath = NSIndexPath(forRow: i, inSection: 0)

            tmpArray.addObject(tmpIndexPath)
        }

        if !sectionExpanded {
            tableView.deleteRowsAtIndexPaths(tmpArray, withRowAnimation: UITableViewRowAnimation.Top)
        } else {
            tableView.insertRowsAtIndexPaths(tmpArray, withRowAnimation: UITableViewRowAnimation.Top)
        }
    } else {
        delegate?.rightItemSelected(selectedItem)
    }
}

既然你知道行数总是 5 或 1,你可以尝试这样的事情。但是,这不是标准方法,我建议更改您的数据源数组。
以下是一些如何操作的示例:http://www.nsprogrammer.com/2013/07/updating-uitableview-with-dynamic-data.html它是针对 Objective-C 的,但您会了解它的要点。

关于ios - 向 UITableView 添加行时更新无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28900230/

相关文章:

ios - 需要有关 textField :shouldChangeCharactersInRange:replacementString: 的帮助

ios - 由嵌入在 UITabBarController 中的 UINavigationController 呈现的 View 不会调整 View 大小以考虑 View 底部的 tabBar

ios - 桥接 header 导入找不到目标。如何正确导入?

ios - 使用 pushwoosh sdk 将应用上传到应用商店的问题

python - Xcode 4.2(内部版本 4D199)+ Python : Console output is different to the expected (e. g。没有 UTF-8 字符)

ios - 在 View Controller 之间切换时图像旋转动画速度加快?

ios - 在 block 之间传递值类型

ios - Pod 安装了 Alamofire 4.4,但是 Xcode 8.3 没有自动完成类

iphone - json返回格式

c++ - XCode 的简单 C++ 项目问题