问题
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