Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange animation when moving last row out of section and deleting section
    primarykey
    data
    text
    <p>I have a multi-section tableview. In edit mode I allow rows to be moved from one section to another. Once the final row is removed from one section I delete that section. So I am using deleteSection inside moveRowAtIndexPath.</p> <p>When the final item is moved from the section, the section header disappears as planned. But there is a very strange animation bug, where the moved row seems to 'merge' with the row it is dropped above, and an empty row is displayed at the bottom of the 'to' section (probably because the numberOfRows for that section is correct, but 2 rows are in the same position). Even stranger, when I click the reorder control for this row (not moving the item, simply touching and releasing), the two items 'unmerge'.</p> <p>I have posted a <a href="http://www.youtube.com/watch?v=WorlOupeR6o" rel="nofollow">video</a> demonstrating this.</p> <p>I have tried wrapping my data changes and view changes in begin/end updates, but to no avail.</p> <p>I have uploaded a test project <a href="http://www.madeinbletchley.com/TestSectionDelete.zip" rel="nofollow">here</a>, and I will also post the code below. A couple of points:</p> <ul> <li>I have tried to replicate my data source's format in the demo project, in case this is where the problem originates. The key thing is that my source is a composite array of two other arrays (though I can't see why this would be an issue).</li> <li>To see the behavior in question, move the two rows in the bottom section, up into the top section. Don't drop them in the last row on the top section though, since this seems to work ok.</li> <li>Moving rows the other way, from the top section to the bottom section, is buggy in this demo project.</li> </ul> <p><strong>Code (all of this is in the demo project):</strong></p> <p>I set up my arrays in loadView:</p> <pre><code>- (void)loadView{ array1 = [NSMutableArray array]; [array1 addObject:@"test 0"]; [array1 addObject:@"test 1"]; [array1 addObject:@"test 2"]; array2 = [NSMutableArray array]; [array2 addObject:@"test a"]; [array2 addObject:@"test b"]; [super loadView]; } </code></pre> <p>I also have a method that returns a combination of these arrays - this is used as the data source:</p> <pre><code>- (NSMutableArray *)sourceArray{ NSMutableArray *result = [NSMutableArray array]; if (array1.count &gt; 0) { [result addObject:array1]; } if (array2.count &gt;0) { [result addObject:array2]; } return result; } </code></pre> <p>Which allows for very simple number of rows/sections:</p> <pre><code> - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return self.sourceArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [[self.sourceArray objectAtIndex:section] count]; } </code></pre> <p>Standard Cell/Header formatting:</p> <pre><code> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.textLabel.text = [[self.sourceArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringWithFormat:@"Section %i", section]; } </code></pre> <p><strong>This is where I do the magic</strong></p> <pre><code> // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { NSMutableArray *fromArray = [self.sourceArray objectAtIndex:fromIndexPath.section]; NSMutableArray *toArray = [self.sourceArray objectAtIndex:toIndexPath.section]; NSString *movedObject = [[self.sourceArray objectAtIndex:fromIndexPath.section] objectAtIndex:fromIndexPath.row]; [fromArray removeObject:movedObject]; [toArray insertObject:movedObject atIndex:toIndexPath.row]; if ([self.tableView numberOfRowsInSection: fromIndexPath.section] == 0) { [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:fromIndexPath.section] withRowAnimation:UITableViewRowAnimationFade]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload