2011-02-01 73 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]; 
    } 
}