【发布时间】:2018-03-29 11:22:30
【问题描述】:
我最近开始从事一个小型 iOS 项目,我有一个由 UITableView 表示的游戏得分排行榜,每当玩家获得新的高分时,排行榜就会可见,并且他的得分条目(包括图片、名称和得分) ) 由UITableViewCell 表示应该移动到它现在所属的TableView 排行榜中的新右侧位置。新的index 的计算工作正常,但cell 根本没有移动。
一些信息:
排行榜填充成功,
我将
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool设置为true还实现了
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
正确。
我在想,也许我遗漏了一些东西,但问题也可能出在其他地方,我不知道,非常感谢一些帮助。
编辑的委托方法
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// Handles reordering of Cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let player = players[sourceIndexPath.row]
players.remove(at: sourceIndexPath.row)
players.insert(player, at: destinationIndexPath.row)
}
我尝试移动行的代码
func newHighscore(highscore: Int) {
friendsTableView.reloadData()
let myNewIndex = Player.recalculateMyIndex(players: players, score: highscore)
friendsTableView.moveRow(at: [0,Player.myIndex], to: [0,myNewIndex])
}
【问题讨论】:
-
在你更新你的新数据源之后,例如具有分数、名称等的对象数组,并且你已经将你的新分数放在数据源上的正确位置,你应该调用方法 self. myTableViewName.reloadData() 它将重绘表格视图
-
使用 insertRow 移动单元格
-
@ΒασίληςΔ。然后它会像在编辑模式下拖动时那样为这个动作设置动画吗?
-
@Cesare 但是插入了我不想做的单元格,我想移动它们还有什么应该移动的:to: 函数是关于当它不将行从 A 移动到 B 时跨度>
-
太难了,使用数组更新可能更好,然后使用 reloadData() ???
标签: ios swift xcode uitableview