2011-02-01 63 views
3

我想更新的UITableView时将滚动到顶部。在facebook应用中,当我们将uitableview滚动到顶部时,它会显示“upadating”并在表格视图顶部插入新行。我想在我的应用程序中发挥同样的效果。有可能使用默认的uitableview方法来设计它,或者我们需要定制它。 如果有人有想法请回复。的UITableView autoupadate而滚动到顶部

回答

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和1两个排在第0

注:您需要定制dataSourcedelegate方法与这些插入调整。重要的是你需要从numberOfRowsInSection方法返回正确的值。这里,在上面的例子中,你numberOfRowsInSection方法应返回yourPreviousNumberOfRows + 2,因为我们在这里添加了两个行。

+0

谢谢..你有任何教程链接呢? – Gaurav 2011-02-01 06:13:52

+0

对不起的人。我没有任何联系的教程..我想简单的谷歌搜索将帮助你.. – EmptyStack 2011-02-01 08:42:30

6

您可能希望当用户滚动到顶部检测:

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

- (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

您现在可以解雇你的事件,这取决于用户的操作: 即 当用户滚动到顶部,使用打电话给你的用户界面的变化,然后调用您的更新逻辑。

insertRowsAtIndexPaths:withRowAnimation: 

样品和资料来源:更新逻辑已经结束http://www.mlsite.net/blog/?p=466

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

+0

滚动视图可以有插图的内容(例如,观点背后的导航栏,而从导航栏的末尾内容开始)。 scrollView.contentOffset.y + scrollView.contentInset.top == 0应该会更好。 – Kazuki 2014-06-16 01:36:15

5

另一种方式:

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