gpt4 book ai didi

ios - 跨部分重新排序行时更新无效

转载 作者:行者123 更新时间:2023-11-28 16:07:25 25 4
gpt4 key购买 nike

我尝试在跨部分重新排序一些 UITableViewCells 时更新我的​​模型,但出现错误:

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

我有以下代码,我认为我在其中正确删除和插入行? (但显然不是 ;-) )

    // Update the data model according to edit actions delete or insert.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
let fromIndexPath = NSIndexPath(forRow: indexPath.row, inSection: indexPath.section)

if editingStyle == UITableViewCellEditingStyle.Delete{
playingList.removeAtIndex(fromIndexPath.row);
}
}

// Process the row move. This means updating the data model to correct the item indices.
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
let moveFrom = NSIndexPath(forRow: sourceIndexPath.row, inSection: sourceIndexPath.section)
let moveTo = NSIndexPath(forRow: destinationIndexPath.row, inSection: destinationIndexPath.section)

// swap the data between the 2 arrays
let dataPiece = playingList[moveFrom.section][moveFrom.row]
playingList[moveTo.section].insert(dataPiece, atIndex: moveTo.row)
playingList[moveFrom.section].removeAtIndex(moveFrom.row)
// Do the move between the table view rows
playerTableView.moveRowAtIndexPath(moveFrom, toIndexPath: moveTo)
}

最佳答案

您的问题是您正在调用 moveRowAtIndexPath。只需更新您的数据模型即可。

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
let moveFrom = NSIndexPath(forRow: sourceIndexPath.row, inSection: sourceIndexPath.section)
let moveTo = NSIndexPath(forRow: destinationIndexPath.row, inSection: destinationIndexPath.section)

// swap the data between the 2 arrays
let dataPiece = playingList[moveFrom.section][moveFrom.row]
playingList[moveTo.section].insert(dataPiece, atIndex: moveTo.row)
playingList[moveFrom.section].removeAtIndex(moveFrom.row)
}

顺便说一句 - 您的 commitEditStyle 方法也不正确。它正在删除整个部分的数据(但使用行而不是部分)。只需删除一行:

playingList[indexPath.section].removeAtIndex(indexPath.row)

关于ios - 跨部分重新排序行时更新无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40073569/

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