滚动到顶部时,uitableview autoupadate

时间:2011-02-01 05:59:42

标签: iphone objective-c ios

我希望在滚动到顶部时更新Uitableview。在facebook应用程序中,当我们将uitableview滚动到顶部时,它将显示“upadating”并在表视图的顶部插入新行。我想在我的应用程序中给出相同的效果。是否可以使用默认的uitableview方法进行设计,或者我们需要对其进行自定义。 如果有人对此有所了解,请回复。

3 个答案:

答案 0 :(得分:7)

您可能想要检测用户何时滚动到顶部:

有几种方法可以做到这一点:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y == 0)
        NSLog(@"At the top");
}

来源:How can I get scrollViewDidScrollToTop to work in a UITableView?

或使用:

scrollViewDidScrollToTop: 

来源:http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollingViewContent/ScrollingViewContent.html

您现在可以根据用户的操作触发事件: 即 当用户滚动到顶部时,使用调用UI更改,然后调用更新逻辑。

insertRowsAtIndexPaths:withRowAnimation:

示例和来源:http://www.mlsite.net/blog/?p=466

更新逻辑结束后,假设您的数据源现已更新,请调用[tableView reloadData]。 (可选)您可能希望使用deleteRowsAtIndexPaths:withRowAnimation:在删除单元格时添加效果,通知您当前正在更新。

答案 1 :(得分:5)

另一种方式:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
            [self fetchMoreMessages];
    }
}

答案 2 :(得分:2)

您可以使用insertRowsAtIndexPaths:在表格视图的前面插入新行。例如,

 NSIndexPath *indexPathForRow_0_0 = [NSIndexPath indexPathForRow:0 inSection:0];
 NSIndexPath *indexPathForRow_0_1 = [NSIndexPath indexPathForRow:1 inSection:0];
 [yourTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathForRow_0_0, indexPathForRow_0_1, nil] withRowAnimation:UITableViewRowAnimationTop];

上面的代码在0的索引0和1处插入两行。

注意:您需要自定义dataSourcedelegate方法,以便根据这些插入进行调整。重要的是您需要从numberOfRowsInSection方法返回正确的值。在上面的示例中,您的numberOfRowsInSection方法应返回 yourPreviousNumberOfRows + 2 ,因为我们在这里添加了两行。