I want to block a cell to be reordered.
For example:
I have a tableview with 4 rows but don't want that the first cell can be reordered.
Is that possible?
I've tried to use:
if(indexPath.row == 0)
{
[cell setEditing:NO animated:NO];
}
But doesn't work.
Thanks.
I found the answer!
You can use the method targetIndexPathForMoveFromRowAtIndexPath from UITableViewDelegate.
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
if (proposedDestinationIndexPath.row == 0) {
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
Your UITableViewDataSource should implement -(BOOL)tableView:canMoveRowAtIndexPath:
and return NO for indexPath.row == 0
- (BOOL)tableView:canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return (indexPath.row != 0);
}
UITableViewDataSource documentation
SWIFT 5 CODE:
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
if proposedDestinationIndexPath.row == 0 {
return sourceIndexPath
}
return proposedDestinationIndexPath
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With