In an iOS table view, how should a row be moved to a new section?

I have a table view with two rows in one section, i.e.

--- Row A Row B 

If I want to animate line 1 into a new section, that is the end result:

 --- Row B --- Row A 

How should this be achieved? I already tried:

 [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow: 0 inSection: 0]] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView insertSections:[NSIndexSet indexSetWithIndex: 1] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView endUpdates]; 

However, at least in iOS 8, the line is animated, but then the new section is not displayed (i.e. white, without the bottom line border) until some time after something causes a redraw and returns.

 [self.tableView beginUpdates]; [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; [self.tableView endUpdates]; 

What causes an exception:

 Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). 

and

 [self.tableView beginUpdates]; [self.tableView insertSections:[NSIndexSet indexSetWithIndex: 1] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; [self.tableView endUpdates]; 

What causes an exception:

 cannot move a row into a newly inserted section (1) 

Should I just not animate at all and reload the whole table?

+5
source share
3 answers

You need to complete this in two steps.

First you need to add a section; you need to tell your source of account information for the new section. For a simple example like yours, you can simply set the variable:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if(hasTwoSections) return 2; return 1; } 

Then, where you want to call the animation, call:

 hasTwoSections = true; // this needs to be set before endUpdates [self.tableView beginUpdates]; [self.tableView insertSections:[NSIndexSet indexSetWithIndex: 1] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; 

At this point, tableView will call the data source methods to update the table based on your changes.

After adding a section, you can move the line to another update block:

 [self.tableView beginUpdates]; [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; [self.tableView endUpdates]; 

Obviously, you will need to update what numberOfRowsInSection returns to account for changes.

Although these are two steps, it will look as if everything happens at once.

+4
source

I was in a similar situation when I got an exception cannot move a row into a newly inserted section (1) . I found only two solutions half .

  • Reload the table with reloadData() - aggressive and not animated
  • Delete and paste your lines, the code below:

 self.requestTableView.deleteRowsAtIndexPaths([oldIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic) self.requestTableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 

To get animation effects that give the impression of moving lines, you can use UITableViewRowAnimation.Top to delete, insert, or reload sections.

 self.tableView.deleteSections(toDelete, withRowAnimation: UITableViewRowAnimation.Top) self.tableView.insertSections(toInsert, withRowAnimation: UITableViewRowAnimation.Top) self.tableView.reloadSections(toReload, withRowAnimation: UITableViewRowAnimation.Top) 
0
source

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


All Articles