删除ios7中TableView的最后一行时animation问题

我在删除tableView中我(仅)部分的最后一行时遇到了一些问题。 其他任何行都可以正常工作,但是如果我随时删除tableView底部的行(而不是最后一行),则animation非常奇怪而且很缓慢。 它看起来不正确。 我也注意到,改变animationtypes不会做任何事情。 animation总是在行向上滑动并消失的时候。 将其更改为UITableViewRowAnimationFade或其他不做任何事情。

这是我的代码

 //For the edit barButtonItem in my storyboard - (IBAction)editButtonPressed:(id)sender { //enter editing mode if ([self.editButton.title isEqualToString:@"Edit"]) { [self setEditing:YES animated:YES]; self.editButton.title = @"Done"; } else { [self setEditing:NO animated:YES]; self.editButton.title = @"Edit"; } } //Editing the tableView. The user can only delete rows, not add any - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // If row is deleted, remove it from the list. if (editingStyle == UITableViewCellEditingStyleDelete) { //Handle the data source and backend first, then the tableView row PFRelation *hasFavorites = [self.currentUser relationforKey:@"hasFavorites"]; [hasFavorites removeObject:[self.favorites objectAtIndex:indexPath.row]]; [self.favorites removeObjectAtIndex:indexPath.row]; //It is set to fade here, but it only ever does the top animation [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //save to the backend [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { } else { NSLog(@"%@ %@", error, error.userInfo); } }]; } } 

我已经看过每一个我找不到的答案。 我在tableview numberOfSections中返回1,因为我只想要一个部分,我应该可以在一个部分有0行,所以我不认为这是问题。

它是ios7的一个bug ..表格animation被打破了! 我的修复是淡出tableViewRowAnimation之前的单元格。

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // hide cell, because animations are broken on ios7 double iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; if (iosVersion >= 7.0 && iosVersion <= 8.0) { [tableView cellForRowAtIndexPath:indexPath].alpha = 0.0; } [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; } 

我发现这个解决scheme在jaydee3的答案基础上运作良好:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // hide cell, because animations are broken on ios7 double iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; if (iosVersion >= 7.0 && iosVersion <= 8.0) { // Animating the cell's alpha change gives it a smooth transition // Durations > .17 show the glitch on iPad, phone looks nice up to 0.3 [UIView animateWithDuration:0.17 animations:^(void) { [tableView cellForRowAtIndexPath:indexPath].alpha = 0.0; }]; } NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects:indexPath, nil]; // UITableViewRowAnimationFade looks nice with the animation imho [tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; } } 

我有同样的错误。 我只是改变了UITableViewRowAnimationMiddle的animationtypes,并修复了这一切。

我的解决这个痛苦的问题:)

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [UIView animateWithDuration:0.25f animations:^{ UIScrollView *internalScrollView = (UIScrollView*)cell.contentView.superview; if([internalScrollView isKindOfClass:[UIScrollView class]]){ [internalScrollView setContentOffset:CGPointZero animated:YES]; } cell.center = CGPointMake(cell.center.x - cell.bounds.size.width, cell.center.y); } completion:^(BOOL finished) { [self.rowModels removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }]; }