【发布时间】:2018-10-31 09:19:44
【问题描述】:
几天前,我从 Stack Overflow 那里得到了很好的建议。这是为了防止最后一个单元格在 UITableView 中移动。它使用以下代码。
func tableView (_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
if indexPath.row <myTableviewArray.count {
return true
}
return false
}
我用来移动单元格的代码是:
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if destinationIndexPath.row != menuDic.count {
let movedObject = menuDic[sourceIndexPath.row]
menuDic.remove(at: sourceIndexPath.row)
menuDic.insert(movedObject, at: destinationIndexPath.row)
}
}
但是出现了一个意想不到的问题。另一个牢房正在最后一个牢房的下方移动!除了最后一个单元格之外的单元格不得移动到最后一个单元格下方。最后一个单元格应该总是最后一个。
我使用了以下两个代码,但都失败了。
func tableView (_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
if proposedDestinationIndexPath.row == myTableviewArray.count - 1 {
return IndexPath (row: myTableviewArray.count - 1, section: 0)
}
return proposedDestinationIndexPath
}
func tableView (_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if destinationIndexPath.row! = myTableviewArray.count {
let movedObject = myTableviewArray [sourceIndexPath.row]
myTableviewArray.remove (at: sourceIndexPath.row)
menuDic.insert (movedObject, at: destinationIndexPath.row)
}
}
使用这两个代码,单元格仍将移动到最后一个单元格的下方。堆栈溢出。谢谢你! :)
【问题讨论】:
标签: ios swift uitableview uitableviewrowaction