gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:05 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com