我想将选定的tabelview单元格移到顶部

时间:2019-04-15 11:12:34

标签: swift uitableview

我正在尝试将tableview选定的单元格移到顶部

    let itemToMove = arrInrestLocation[indexPath.row]
    arrInrestLocation.remove(at: indexPath.row)
    arrInrestLocation.append(itemToMove)
    let destinationindexPath = NSIndexPath(row: arrInrestLocation.count - 1, section: indexPath.section)
    tableView.moveRow(at: indexPath, to: destinationindexPath as IndexPath)

1 个答案:

答案 0 :(得分:0)

您正在将行和项目移到底部而不是顶部。您只需要稍微编辑一下代码。表部分的第一行是零行。 append方法将该项添加到数组的末尾,您需要将其插入数组的开头。

 let itemToMove = arrInrestLocation[indexPath.row]
 arrInrestLocation.remove(at: indexPath.row)
 arrInrestLocation.insert(itemToMove, at: 0) //move to front of array
 let destinationindexPath = NSIndexPath(row: 0, section: indexPath.section) 
 tableView.moveRow(at: indexPath, to: destinationindexPath as IndexPath)