How to swap two custom cells with one another in tableview?

廉价感情. 提交于 2019-12-08 06:32:15

问题


I have implemented two custom cells in the tableview.Now I want to replace these two custom cells with one another. How to achieve this?


回答1:


Implement following tableview methods and write your code in it

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
}

Share your full source code better help




回答2:


If you would like to reorder rows in UITableView. Take a look at two delegates that are implemented in the UITableView.

They are: tableView:canMoveRowAtIndexPath: and moveRowAtIndexPath:toIndexPath:.

Also take a look at tutorial that show how it is possible to implement.




回答3:


Along with tableview delegates, use moveRow(at: <IndexPath>, to: <IndexPath>), to move your row programatically (automatically), without user interaction.

tblTabel.moveRow(at: <IndexPath>, to: <IndexPath>)    


// 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
}


来源:https://stackoverflow.com/questions/42552683/how-to-swap-two-custom-cells-with-one-another-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!