繁体   English   中英

在底部添加单元格并滚动UITableView

[英]Add cell at bottom and scroll UITableView

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

我试过这样做:

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

但是这会造成图形故障并且表格会闪烁,并且会产生错误的滚动而不是平滑的滚动。

对此有何帮助?

这里我做的是,插入一行然后更改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];
}

添加项目的示例函数:

- (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];
}

对我来说,动画看起来非常精致!

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

将插入设置为动画,并在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM