在nstableview列中将nil值行总是排在底部 [英] Sorting nil-valued rows always to bottom in nstableview column
问题描述
我在我的应用程序中有一个 NSTableview
包含几列。当我点击一个列标题以便按升序排序时,在该列中具有nil值的行将按顶部排序,然后按升序排列非零值行。如果我在同一列上重新点击,首先按降序显示非nil值的行,然后是nil值行。
I have an NSTableview
in my application containing several columns. When I click on one column header in order to have it sorted ascending, rows with a nil-value in that column are being sorted on top followed by non-nil-valued rows in ascending order. If I reclick on the same column, first the non-nil-valued rows are being shown in descending order followed by nil-valued rows.
我想要做的是具有零值行总是粘在底部。因此,首先应该呈现以升序或降序排序的非零值行,并且应该将nil值行保持在底部。
What I want to do is having nil-valued rows always sticked to the bottom. So at first non-nil-valued rows being sorted in either ascending or descending way should be presented and nil-valued rows should be kept at the bottom. How can this be done the best way?
在这个上下文中排序 - 是我的理解 - 是由 NSArrayController
arrangedObject:
。因此,我应该将 NSArrayController
子类化,并在 arrangeObjects:
方法中使用自定义排序算法,上述结果?
Sorting in this context - to my understand - is done by the NSArrayController
's arrangedObject:
. So should I subclass NSArrayController
and use a custom sorting algorithm in arrangedObjects:
method, or is there a better way to get the result described above?
推荐答案
非常感谢。正如我上面提到的,我总是希望nil值排序到底部我用compareObject:以下面的方式使用你的代码。
Thanks a lot. As I mentioned above I always want nil-values sorted to the bottom I used your code with compareObject: in the following way.
- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2 {
if (NULL_OBJECT([object1 valueForKeyPath:[self key]]) && NULL_OBJECT([object2 valueForKeyPath:[self key]]))
return NSOrderedSame;
if (NULL_OBJECT([object1 valueForKeyPath:[self key]]))
return NSOrderedDescending;
if (NULL_OBJECT([object2 valueForKeyPath:[self key]]))
return NSOrderedAscending;
return [super compareObject:object1 toObject:object2];
}
这篇关于在nstableview列中将nil值行总是排在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!