gpt4 book ai didi

swift - 重新排序 UITableView 单元格并跟踪索引

转载 作者:行者123 更新时间:2023-11-30 13:51:04 25 4
gpt4 key购买 nike

我有一个单词数组,它们以特定顺序显示在 UITableView 中 word1、word2、word3、word4、word5 等。核心数据中每个单词都有一个称为 wordIndex 的属性,用于跟踪每个单词的索引因此用户可以编辑表格并更改词序。我试图在每次移动时使用 moveRowAtIndexPath 更新表中所有单词的 wordIndex,但此方法似乎不使用 indexPath.row。预先感谢是否有人找到了一种巧妙的方法来做到这一点?

我的代码是:

  func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let wordToMove = words[sourceIndexPath.row]
words.removeAtIndex(sourceIndexPath.row)
words.insert(wordToMove, atIndex: destinationIndexPath.row)

wordToMove.wordIndex = destinationIndexPath.row // seems to always have an index of 2
// and what about updating all the other indexes above and below the moved word?
// indexPath.row is not allowed in this method to iterate over all the rows in the table....

for words[(indexPath.row) == 0]; words[(indexPath.row) = words.count]; words[(indexPath.row)++] {
word?.wordIndex = indexPath.row
}
tableView.reloadData()
coreDataStack.saveMainContext()
}

最佳答案

改用fromIndexPath.rowtoIndexPath.row

关于swift - 重新排序 UITableView 单元格并跟踪索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34255988/

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