开发者

How do I add or remove an off-screen row at the top of a Table View without scrolling?

I have a UITableView containing a list of items.

When the user taps the Edit UIBarButtonItem, the top row needs to be removed, because it is not editable.

I do that like this:

- (void) setEditing: (BOOL) editing animated: (BOOL) animated
{   
    if (editing)
    {
        [self.theTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 0 inSection: 0]] withRowAnimation: UITableViewRowAnimationTop];
        // call super afterward so the user doesn't see the row get an editing accessory before it disappears
        [super setEditing: editing animated: animated];
    }
    else
    {
        [super setEditing: editing animated: animated];
        [self.theTableView insertRowsAtIndexPaths: [NSArray arrayWi开发者_StackOverflowthObject: [NSIndexPath indexPathForRow: 0 inSection: 0]] withRowAnimation: UITableViewRowAnimationTop];
    }
}

This code (unrelated bits snipped) works just fine, with one caveat:

If the user is anywhere but at the top of the table (assuming there is more than a single screenful of row), the animation behavior causes the entire table view to scroll up (or down), and the behavior of the last row in the table is, at best, usable.

My question: How can I only make the top row animate out when the user can see it, but when the user can't see it, it just disappears, but does not cause the table to scroll.


Have you tried UITableViewRowAnimationBottom instead of UITableViewRowAnimationTop?

Otherwise you can manually set the contentOffset property of the UITableView to adjust for the missing row.


Try using UITableViewRowAnimationRight. That should make the cell slide out if it's visible, but otherwise leave your table intact.


Before you do the animation check if the first row is visible.

If yes, peoceed.

Else, you want it not to appear when the user scrolls up, right? so replace the table's data source with one without the first row and after editing ends return the first row to that data source.

Another option might be not displaying it by manipulating -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath But that is probably a bad practice.


There doesn’t appear to be any simple solution to this. I’ve had limited success with completely custom behavior in a UICollectionView, but major versions of iOS and years later, even Apple’s own built-in apps continue to get this edge case wrong.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜