如何在tableview中相互交换两个自定义单元格?

我在tableview中实现了两个自定义单元格。现在我想要将这两个自定义单元格相互替换。 怎么做到这一点?

实现以下tableview方法并在其中编写代码

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // return boolean value for a specific or all cells, you want to enable movement } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { // Handle array element movement along with your row/cell } 

分享您的完整源代码更好的帮助

如果您想在UITableView重新排序行。 看一下在UITableView中实现的两个UITableView

它们是: tableView:canMoveRowAtIndexPath:moveRowAtIndexPath:toIndexPath: .

另请参阅教程 ,了解如何实现。

与tableview委托一起使用moveRow(at: , to: ) ,以编程方式(自动)移动行,无需用户交互。

 tblTabel.moveRow(at: , to: ) // Tableview Delegates func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // return boolean value for a specific or all cells, you want to enable movement } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { // Handle array element movement along with your row/cell }