我有一个单词数组,以特定顺序word1,word2,word3,word4,word5等显示在UITableView中。核心数据中的每个单词都有一个名为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()
}
答案 0 :(得分:0)
改为使用fromIndexPath.row
和toIndexPath.row
。