我也有這個問題。 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
}
你不想滾動你的tableview? –
@hussainShabbir這是滾動沒有任何用戶交互。當我添加一個新行時,我不希望它自動滾動到現在正在執行的中間。 – Jeff
@Jeff解決這個問題有什麼好運?我有同樣的問題。使用自我大小的細胞,它瘋狂地跳躍。 –