2011-02-10 8 views
6

NSTableViewがあり、ユーザーが下部にスクロールしたときを知りたいので、アクションを実行できます。これについてどうやって行くのかは分かりません。 UPDATEユーザーがNSTableViewの最後までスクロールしたかどうかを判断する方法

-(void)tableViewDidScroll:(CPNotification) notification 
{ 
    var scrollView = [notification object]; 
    var currentPosition = CGRectGetMaxY([scrollView visibleRect]); 
    var tableViewHeight = [messagesTableView bounds].size.height - 100; 

    //console.log("TableView Height: " + tableViewHeight); 
    //console.log("Current Position: " + currentPosition); 

    if (currentPosition > tableViewHeight - 100) 
    { 
     console.log("we're at the bottom!"); 
    } 
} 

答えて

14

あなたは(、NSNotificationCenterの意味ではないKVO /バインディングセンスを)オブザーバーとして自分自身を追加することができますNSViewBoundsDidChangeNotificationのから:ここに は、私はテーブルの下を計算しています方法ですテーブルの-enclosingScrollViewの-content可視の矩形に基づいて必要に応じて表示し、反応します。

更新

このどこか(多分-awakeFromNib)を行います。

// Configure the scroll view to send frame change notifications 
id clipView = [[tableView enclosingScrollView] contentView]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(myBoundsChangeNotificationHandler:) 
              name:NSViewBoundsDidChangeNotification 
              object:clipView]; 

が便利なこのどこかを入れて:

- (void)myBoundsChangeNotificationHandler:(NSNotification *)aNotification 
{ 

if ([aNotification object] == [[tableView enclosingScrollView] contentView]) 
    [self doSomethingInterestingIfDocumentVisibleRectSatisfiesMe]; 

} 

は基本的にあなたが見るためにスクロールビューの-documentVisibleRectを調べたいですおそらくピクセルの下のカップルが表示されている場合。 Views Programming Guideに記載されている、反転された座標系のビュー(「フリップビュー」)の可能性を考慮してください。

+0

あなたはスニペットを表示することができます: 常に同じ値が、私はNSClipView境界を使用する方が良いことがわかりましたか? –

+0

投稿を更新しました。 –

+0

これはうまくいきます。下のピクセルが表示されているかどうか計算する際に少し助けてくれますか? –

0

アップデートについて: いくつかの理由で があります。var currentPosition = CGRectGetMaxY([scrollView visibleRect]);

NSClipView *clipView = ...; 
NSRect newClipBounds = [clipView bounds]; 
CGFloat currentPosition = newClipBounds.origin.y + newClipBounds.size.height; 
関連する問題