2013-11-22 29 views
6

我有一個表格視圖,顯示通過網絡獲取的消息。當插入新行時,TableView會滾動到中間

當有新消息進來時,它應該被添加到底部的tableview中。

當新消息進來時,它會正確添加到tableview的底部,但tableview會立即滾動到中間。 (所以,如果我現在已經說了100個單元格,那麼單元格50現在將處於滾動視圖的中間)。

奇怪的是,這隻有在插入之前的滾動點位於tableview的下半部分時纔會發生。所以,如果我在查看99單元格10,並添加一行,它會被添加到底部就好了。但是,如果我在查看99的單元格75,並添加一行,則滾動位置又跳到中間(〜單元格50)。

這是我添加新信息的實現代碼如下代碼:

​​

爲什麼實現代碼如下這樣的自動滾動?

+0

你不想滾動你的tableview? –

+0

@hussainShabbir這是滾動沒有任何用戶交互。當我添加一個新行時,我不希望它自動滾動到現在正在執行的中間。 – Jeff

+0

@Jeff解決這個問題有什麼好運?我有同樣的問題。使用自我大小的細胞,它瘋狂地跳躍。 –

回答

0

我認爲你需要改變的行動畫無 -

[self.tableView 
insertRowsAtIndexPaths:indexPaths 
withRowAnimation: 
UITableViewRowAnimationNone]; 
+0

我試過所有的動畫類型,所有的都有相同的結果:插入後,tableview向上滾動到中心。 – Jeff

0

試試這個

[self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

也可以改爲middile,頂部或沒有位置。

0

我也有這個問題。 NSTableView和UITableView可能類似......它們的默認滾動行爲都是SUCK。 TableView並非設計用作電子表格樣式編輯器, ,因此多年來我添加了數千行代碼,使它像電子表格一樣工作 工作。 我希望蘋果將只創建NSTableView的一個NSSpreadsheetView子類來解決這一切的東西: - 光標鍵導航細胞 - 明智的滾動行爲 - 添加特殊單元選擇

還想看到NSTableView的的NSLogView子類,像Console.app一樣具有搜索字段。

我的解決辦法是添加幾個類別方法:

@implementation NSTableView (VNSTableViewCategory) 

-(NSInteger)visibleRow 
{ 
    NSRect theRect = [self visibleRect]; 
    NSRange theRange = [self rowsInRect:theRect]; 
    if (theRange.length == 0) 
     return -1; 
    else if (NSLocationInRange([self editedRow], theRange)) 
     return [self editedRow];   
    else if (NSLocationInRange([self clickedRow], theRange)) 
     return [self clickedRow];  
    else if (NSLocationInRange([self selectedRow], theRange)) 
     return [self selectedRow]; 
    else 
     return theRange.location + (theRange.length/2); 
} 

-(NSRange)visibleRows 
{ 
    NSRect theRect = [self visibleRect]; 
    NSRange theRange = [self rowsInRect:theRect]; 
    return theRange; 
} 

-(void)scrollRowToVisibleTwoThirds:(NSInteger)row 
{ 
    NSRect theVisRect = [self visibleRect]; 
    NSUInteger numRows = [self numberOfRows]; 
    NSRange visibleRows = [self rowsInRect:theVisRect]; 
    //NSUInteger lastVisibleRow = NSMaxRange(visibleRows); 
    if (NSLocationInRange(row, visibleRows)) 
    { 
     if (row - visibleRows.location < (visibleRows.length * 2/3)) 
     { 
     // may need to scroll up 
     if (visibleRows.location == 0) 
      [self scrollRowToVisible:0]; 
     else if ((row - visibleRows.location) > 2) 
      return; 
     } 
    } 

    NSRect theRowRect = [self rectOfRow:row]; 

    NSPoint thePoint = theRowRect.origin; 
    thePoint.y -= theVisRect.size.height/4; // scroll to 25% from top 

    if (thePoint.y < 0) 
     thePoint.y = 0; 

    NSRect theLastRowRect = [self rectOfRow:numRows-1]; 
    if (thePoint.y + theVisRect.size.height > NSMaxY(theLastRowRect)) 
     [self scrollRowToVisible:numRows-1]; 
    else 
    { 
     [self scrollPoint:thePoint]; // seems to be the 'preferred' method of doing this 

     // kpk note: these other approaches cause redraw artifacts in many situations: 
//  NSClipView *clipView = [[self enclosingScrollView] contentView]; 
//  [clipView scrollToPoint:[clipView constrainScrollPoint:thePoint]]; 
//  [[self enclosingScrollView] reflectScrolledClipView:clipView]; 
//  [self scrollRowToVisible:row]; 

//  [[[self enclosingScrollView] contentView] scrollToPoint:thePoint]; 
//  [[self enclosingScrollView] reflectScrolledClipView:[[self enclosingScrollView] contentView]];  
    } 

} 
@end // NSTableView (VNSTableViewCategory) 

用法示例:

NSRange visRows = [toScenesTable visibleRows]; 
    visRows.length -= 2; 
    if (iAlwaysScroll == YES || !NSLocationInRange(row, visRows)) 
    { 
    [toScenesTable scrollRowToVisibleTwoThirds:row]; // VNSTableViewCategory 
    } 
相關問題