【问题标题】:reload uitableview to bottom cell将uitableview重新加载到底部单元格
【发布时间】:2011-03-11 09:30:59
【问题描述】:

我正在创建一个聊天应用程序,并通过 uitableview 中的一个单元格显示每条消息。但是,每次我重新加载 tableview 时,它都会显示第一个单元格的内容。我想安排我的表格,以便在重新加载时,它会一直“滚动”到下方,从而显示最新消息。

我对文档进行了一些研究,但我对编程非常陌生,所以我不知道如何设置它。我想我需要 reloadRowsAtIndexPaths 方法,但我不确定如何实现它。谢谢!

【问题讨论】:

  • 嗨,杰夫,如果我的回答解决了您的问题,请接受。

标签: iphone objective-c ios uitableview scroll


【解决方案1】:

您应该在表格视图的末尾插入另一行,当您这样做时,您需要知道最后一行的编号。

所以你可以调用这个函数来插入新行

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

喜欢这个

[tableView insertRowsAtIndexPaths:[NSIndexPath indexPathForRow:lastRow inSection:0] withRowAnimation:UITableViewRowAnimationNone];
lastRow++;

然后通过调用此函数为表格视图设置动画以滚动到最后一行。

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

喜欢这个

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:0] atScrollPosition:UITableViewScrollPositionButtom animated:YES];

【讨论】:

    【解决方案2】:

    使用这个

    int lastRowNumber = [tableView numberOfRowsInSection:0]-1;
    NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
    [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];
    

    希望这能解决你的问题。

    【讨论】:

    • 这一行+1,[tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];我使用它并达到了我的要求
    【解决方案3】:

    使用–scrollToRowAtIndexPath:atScrollPosition:animated: 方法可以轻松实现滚动表格视图:

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1000 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

    当然,你需要调整 indexPath 的值。

    【讨论】:

      【解决方案4】:

      [self.tableView reloadData]; [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 2012-04-10
        • 1970-01-01
        • 2015-08-01
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多