2015-11-03 78 views
1

我想在UITableView的底部添加单元格,并滚动以使其完全可见(就像在发送或接收消息时在Whatsapp中一样)。在底部添加单元格并滚动UITableView

我试着这样做:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

但是这使得图形毛刺和表闪烁,做一个坏的滚动,而不是平滑的。

对此有何帮助?

+2

我不是在一个位置肯定地说,但它可能是WhatsApp的实际滚动到的tableView顶(0,0)。这是一些消息传递应用程序用来旋转tableView然后每个单元旋转180度的技巧。这给出向下滚动的视觉效果,但滚动的程序化视图。 – Avi

+1

改变动画:是动画:否 – Subash

+0

Subash的建议工程。你不能同时调用两个动画。 – Dave

回答

0

例如函数中添加一个项目:

- (IBAction)addItem:(UIBarButtonItem *)sender { 
    [self.items addObject:[NSString stringWithFormat:@"Item %lu", self.items.count + 1]]; 

    NSIndexPath *indexPathToInsert = [NSIndexPath indexPathForRow:self.items.count - 1 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPathToInsert] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [self.tableView scrollToRowAtIndexPath:indexPathToInsert atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 

我的动画看起来完全没有这样!

+0

我试过了,但整个表格视图在闪烁的同时滚动......感谢反应 – facumedica

0

那么,我所做的是使用[self.tableView scrollToRowAtIndexPath...,但从0.1s NSTimer调用它。

2

在这里我做了什么时,插入的行,然后改变了的tableView

的内容插图
-(IBOutlet)sendButton:(id)sender 
{ 
    [array addObject:textView.text]; 
    [self addAnotherRow]; 
} 
- (void)addAnotherRow { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(array.count - 1) inSection:0]; 
     [self.messagesTableView beginUpdates]; 
     [self.messagesTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.messagesTableView endUpdates]; 
     [self updateContentInsetForTableView:self.messagesTableView animated:YES]; 

     // TODO: if the scroll offset was at the bottom it can be scrolled down (allow user to scroll up and not override them) 

     [self.messagesTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 

- (void)updateContentInsetForTableView:(UITableView *)tableView animated:(BOOL)animated { 
    NSUInteger lastRow = [self tableView:tableView numberOfRowsInSection:0]; 
    NSLog(@"last row: %lu", (unsigned long)lastRow); 
    NSLog(@"items count: %lu", (unsigned long)array.count); 

    NSUInteger lastIndex = lastRow > 0 ? lastRow - 1 : 0; 

    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:lastIndex inSection:0]; 
    CGRect lastCellFrame = [self.messagesTableView rectForRowAtIndexPath:lastIndexPath]; 

    // top inset = table view height - top position of last cell - last cell height 
    CGFloat topInset = MAX(CGRectGetHeight(self.messagesTableView.frame) - lastCellFrame.origin.y - CGRectGetHeight(lastCellFrame), 0); 

    // What about this way? (Did not work when tested) 
    // CGFloat topInset = MAX(CGRectGetHeight(self.tableView.frame) - self.tableView.contentSize.height, 0); 

    NSLog(@"top inset: %f", topInset); 

    UIEdgeInsets contentInset = tableView.contentInset; 
    contentInset.top = topInset; 
    NSLog(@"inset: %f, %f : %f, %f", contentInset.top, contentInset.bottom, contentInset.left, contentInset.right); 

    NSLog(@"table height: %f", CGRectGetHeight(self.messagesTableView.frame)); 

    UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState; 
    [UIView animateWithDuration:animated ? 0.25 : 0.0 delay:0.0 options:options animations:^{ 
     tableView.contentInset = contentInset; 

    } completion:^(BOOL finished) { 
    }]; 
} 

注:查看从阵列现有的讯息,你必须包括这 也。

- (void)viewDidLayoutSubviews { 
    [self updateContentInsetForTableView:self.messagesTableView animated:NO]; 
} 
1

使动画的插入和endUpdates滚动底部后没有动画。工作对我来说

斯威夫特

self.tableView.beginUpdates() 
self.tableView.insertRowsAtIndexPaths(newIndexPaths, withRowAnimation: UITableViewRowAnimation.Top) 
self.tableView.endUpdates() 
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.tableDataCount-1, inSection: 0), atScrollPosition: .Bottom, animated: false) 
相关问题