2011-07-21 40 views
9

我想檢測用戶是否滾動到UITableView的底部,以便我可以做一些額外的東西。爲了正確計算事情,我需要獲取UITableView的可見矩形。我怎樣才能做到這一點?如何獲得UITableView的可見矩形?

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 

    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView]; 

    int currentMaxPosition = CGRectGetMaxY([self.tableView visibleRect]); 
    int currentMinPosition = CGRectGetMinY([self.tableView visibleRect]); 

    int tableViewBottom = [self.tableView bounds].size.height - 100; 
    int tableViewTop = 0; 

    //get older messages once we're near the bottom 
    if (currentMaxPosition > tableViewBottom - 100) 
    { 
     NSLog(@"WE AT THE BOTTOM!"); 
    }  

} 

回答

2

你可以檢查在[tableView visibleCells]獲得可見的細胞或tableView indexPathsForVisibleRows],並確定用戶是否滾動至底部。

+0

基於visiblecell我如何確定用戶是否在底部? –

+0

使用' - (NSArray *)indexPathsForVisibleRows'獲取這些行的索引路徑 – 2011-07-21 14:58:56

0
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 

     int i=0; 

     i=indexPath.row; 

     int count=[urTablearray count]; 

     if (count == (++i)) { 

       //do ur stuff here 

     } 


    } 
+0

代碼無法編譯,您不能修改像indexPath.row這樣的只讀屬性。 –

+0

立即結賬............... –

+0

是否有效 –

0

如果你想要加載更多的細胞,當你到達底部試試這個post

基本上,你知道有多少行目前在表視圖,多少節等,使用方法:你可以

- (NSInteger)numberOfSections 

- (NSInteger)numberOfRowsInSection:(NSInteger)section 

也使用:

- (NSArray *)indexPathsForVisibleRows 

找出索引路徑靠近底部的細胞..

然後使用以下方法UITableViewDelegate

- (void)tableView:(UITableView *)tableView 
    willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath 

並使用indexPath值並將其與「接近」底部索引進行比較。如果靠近底部,請執行一些操作,例如加載更多內容。

某些應用程序對manually load additional content有專門的「加載更多...」單元格。這種方法更容易實現,它會驗證應用中的「加載更多內容」功能。

18

UITableView只是一個UIScrollView子類,因此所有常用的UIScrollView方法都適用,例如,一個UITableView的可見矩形僅僅是它的界限:

CGRect visibleRect = [myTableView bounds]; 

中的visibleRect的起源僅僅是contentOffset,所以你可以使用另一種方法是:

CGFloat distanceFromBottom = [self.tableView contentSize].height - [self.tableView contentOffset].y; 
+0

距離底部總是0當我打印它 –

+0

正是我在找的,謝謝! –

+0

優秀。 ' - [UITableView bounds]'是完美的,如果由於某種原因需要tableview的可見矩形而不是全長(這將包括不可見行)的內容rect(在這種情況下使用' - [UITableView frame]')。 –

2

我試圖顯示圖像當我點擊一個單元格時,我想找到框架以顯示它(獲取tableview的當前位置)。

我用下面的和它的工作:

//originalImageViewFrame is the CGRect that I use for my new view that I want to add to the view. 
    originalImageViewFrame.origin = CGPointZero; 
    originalImageViewFrame.size = self.tableView.contentSize; 

希望它幫助。

相關問題