开发者

Redoing UITableView layout when user taps Edit button?

开发者 https://www.devze.com 2022-12-19 07:48 出处:网络
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.

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:comm开发者_开发知识库itEditingStyle: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.


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];


If you wish for your table cells to change their format in response to whether or not the table is in editing mode, you could override -setEditing:animated: in your UITableViewController and trigger a reload (via -reloadData) of the table view on a change of editing state.

Within your UITableViewController's -tableView:cellForRowAtIndexPath: method, you could check for whether or not the table was in the editing state by querying the editing property on the table view, and then return a different cell type depending on which state the table is in.

0

精彩评论

暂无评论...
验证码 换一张
取 消