How to update UITableViewCell upon re-order

When you drag a UITableViewCell , the delegate method is called - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath . Within this delegate method, I can set the properties for the selected cell depending on its proposedDestinationIndexPath .

My question now is how to update the contents of a cell while it is being moved. I can update the cell after releasing the move descriptor, but I will need to show the updated values ​​when moving up and down depending on its position ( proposedDestinationIndexPath ).

Does anyone have an idea how to achieve this?

+4
source share
1 answer

You can change the contents of a cell in tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:sourceIndexPath]; cell.textLabel.text = [NSString stringWithFormat:@"Moving Cell to %d %d", proposedDestinationIndexPath.section, proposedDestinationIndexPath.row]; return proposedDestinationIndexPath; } 

However, unless you are actually trying to move the cell to a new indexPath, this method is not called. Therefore, if you simply move the cell a few pixels up and down, you cannot change your text. But I think you should not do this.

+3
source

Source: https://habr.com/ru/post/1402111/


All Articles