Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redoing UITableView layout when user taps Edit button?

I have a UITableView with complex content. The user can edit (rearrange and delete) the cells when tapping the Edit button the standard way. But I want the cells to look different in "edit" mode.

Question:

How to change the UITableView Layout in edit mode, including changing row height?

So far, this is what I have:

The Edit button sends a WillTransitionToState/DidTransitionToState message to each uitableviewcell (UITVC). I have subclassed UITVC and react to these inside each cell, hiding and removing and reshuffling as needed. But, changing the row height is beyond the scope of one cell.

There does not seem to be a message sent to UITableView when user taps edit. There is a - tableView:commitEditingStyle:forRowAtIndexPath: sent to data source after editing a particular row.

Inside heightForRowAtIndexPath, I can query the current mode using the tableView.editing property, and report height as appropriate. And I can trigger re-flowing the table, including recomputing the heights, by invoking [tableView reloadData]. But, when do I call it?

I could send messages from the cells from within WillTransitionToState back to the "owning" table view, and call reloadData when I get them. But this sounds fragile and there must be a better way.

like image 588
Jaanus Avatar asked Sep 03 '26 02:09

Jaanus


1 Answers

Rhythmic is right. Using reloadData kills the nice editing animation.

This problem is addressed in this post: Can you animate a height change on a UITableViewCell when selected?

Instead of using reloadData, do the following after calling setEditing:animated.

[tableview setEditing:editing animated:YES];
[tableview beginUpdates];
[tableview endUpdates];
like image 99
Edward E. Lau Avatar answered Sep 05 '26 18:09

Edward E. Lau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!