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?
Asked
Active
Viewed 1,069 times
3 Answers
0
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
-
I want to swap these custom cell automatically .. Not by the user itself. – akshay Mar 02 '17 at 10:46
0
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.

Oleg Gordiichuk
- 15,240
- 7
- 60
- 100
-
By using this method user can swap the cells but i want that it will swap automactically.. I have two cell just want to replace with one another.Thanks for your quick reply. – akshay Mar 02 '17 at 10:48
0
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
}

Krunal
- 77,632
- 48
- 245
- 261