2011-05-24 72 views
9

當一個表格有很多行時,用戶可以向上/向下滑動表格。這會創建一個滾動動畫,該動畫看起來具有確定的長度,具體取決於輕拂手勢的速度/長度。如果沒有進一步的用戶交互,一旦滾動停止,是否可以可靠地計算出表格中哪些行將可見?計算UITableView停止滾動的行數?

+0

好問題,但據我所知道的..不,似乎這將是一個複雜的操作來保持周圍的東西。 – 2011-05-24 21:18:21

回答

-1

我不知道如何確定將顯示多少行,但總是可以獲取顯示的行數。 (一旦停止表瓦特/沒有進一步的接觸) 不知道有沒有什麼幫助,但是這是你會怎麼做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    // make sure to declare your integer in your .h file and to also synthesize 
    //say this is your int "howManyRowsAreShowing" 
    howManyRowsAreShowing = indexPath.Row; 



    //the rest of the code below is generic table view code for example only 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row]; 
    cell.text = cellValue; 

    return cell; 
    } 
0

有趣的問題..... UITableViewDelegate符合UIScrollViewDelegate還有:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/UIScrollViewDelegate

您可以使用一些代表回調來了解滾動何時開始減速,並結束減速。

您大概可以使用– scrollViewDidEndDecelerating:,然後在此處使用tableView(tableView子類UIScrollView)的單元高度和內容偏移屬性,然後計算減速後可見的單元。

1

UITableViewUIScrollView繼承,並可以完成,通過使用UIScrollViewDelegate方法和表視圖indexPathsForVisibleRows屬性來檢查其細胞指數徑在滾動停止的那一刻可見。

甚至可以保存減速開始位置的初始位置,以便計算滾動方向是上升還是下降,然後可以讓您知道是停止的單元格是第一個還是第可見的最後一個。

int startDeceleratingPosition; 

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

    startDeceleratingPosition = scrollView.contentOffset.y; 

} 

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

    BOOL isPositionUp = startDeceleratingPosition < scrollView.contentOffset.y;  

    NSArray *paths = [_myTableview indexPathsForVisibleRows]; 
    UITableViewCell *cell; 
    if(isPositionUp){ 
     cell = [_myTableview cellForRowAtIndexPath:[paths objectAtIndex:0]]; 
    } else { 
     cell = [_myTableview cellForRowAtIndexPath:[paths lastObject]]; 
    } 

} 

關於上面的代碼中一個重要的注意的是,它指向的表視圖作爲變量_myTableview,而不是僅僅鑄造委託方法變量scrollViewUITableView *,雖然這只是實現的細節,應該不會影響邏輯在這裏。

+0

啊,如果只有一種方法來獲得滾動的速度... – 2011-05-31 07:38:03

+0

但是有這樣一種方式... UIScrollView有一個減速度屬性,這只是一個低級別的方法,並且還有一個很多機會得到錯誤的結果。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/doc/uid/TP40006922-CH3-DontLinkElementID_2 – 2011-05-31 23:12:40

+0

減速率只是一個常數,像重力。您需要知道當前的滾動速度,以便進一步計算滾動停止的時間和位置,並且我沒有看到通過公共API獲取滾動速度的任何方法。 – 2011-06-01 09:04:07