Animation issue when deleting the last row of UITableView

后端 未结 5 853
傲寒
傲寒 2021-02-05 19:19

UPDATE: This is now fixed in iOS 8.0 and above. See my accepted answer for details.

I have an iOS 7 UITableView that I allow swipe-to-delete on rows. I\'m handling delet

相关标签:
5条回答
  • 2021-02-05 19:31

    You can also use the UITableView.tableFooterView property:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        ... 
    
        CGRect frame = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        UIView *view = [[UIView alloc] initWithFrame:frame];
        view.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tableFooterView = view;
    }
    

    This code goes into the view controller holding the UITableView.

    • Code assumes ARC.
    • It's a different story if you use background image for the table.
    • The code can be surrounded by "if (iOS > 7)" clause.
    0 讨论(0)
  • 2021-02-05 19:38

    UPDATE: This bug has been corrected in iOS 8. That final cell deletion now slides off to the left, the delete button slides up and away, and the background is clear (no more white area that abruptly disappears after the animations complete). The iOS 7 fix below is still needed when running below iOS 8.

    iOS 7 Fix: I was able to correct this problem by adding another section to the end of the table with a sufficiently tall section header view. This header view is styled to look like the blank area at the bottom of the table, so you can't see that it's there. When the last row of the table is deleted, this blank section header slides up and over it, hiding the delete button that's stuck there. This is a bit of a hack, but it looks like it's a table view bug.

    0 讨论(0)
  • 2021-02-05 19:39

    my solution was to simply disable the broken animation by hiding the cell that's being deleted.

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.hidden = YES;
    }
    
    0 讨论(0)
  • 2021-02-05 19:42

    As an alternative to the keep-an-empty-row trick mentioned by Frank Li, I've gotten around this ugly glitch by simply special-casing the deletion of the last row: Instead of animating the cell away, simply call -reloadData on the table view.

    You won't get any animation, but you also won't see the glitch so your table view won't look broken. A worthwhile tradeoff.

    0 讨论(0)
  • 2021-02-05 19:47

    I think a way-around is to always maintain an empty row below the last row in the table view. That will do the trick.

    0 讨论(0)
提交回复
热议问题