我正在Objective-C中开发一个macOS应用程序。我想添加一个功能来对NSTableView进行排序,以在其他项目之前显示所选项目。我的代码是:
NSUInteger idx = [self.itemsTableView.selectedRowIndexes firstIndex];
NSMutableArray *selectedObjects = [NSMutableArray new];
while (idx != NSNotFound)
{
// do work with "idx"
// NSLog (@"The current index is %lu", (unsigned long)idx);
[selectedObjects addObject:self.items[idx]];
// get the next index in the set
idx = [self.itemsTableView.selectedRowIndexes indexGreaterThanIndex:idx];
}
self.items = [self.unFilteredItems sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
BOOL obj1Exists = [selectedObjects containsObject:obj1];
BOOL obj2Exists = [selectedObjects containsObject:obj2];
if (obj1Exists && !obj2Exists) {
return NSOrderedAscending;
}
if (obj1Exists && obj2Exists) {
return NSOrderedSame;
}
if (!obj1Exists && !obj2Exists) {
return NSOrderedSame;
}
if (!obj1Exists && obj2Exists) {
return NSOrderedDescending;
}
return NSOrderedSame;
}];
[self.itemsTableView reloadData];
不幸的是,在大约70.000个项目和4500个项目中,比较器花费了太多时间。有人可以给我指出这个问题的替代解决方案吗?感谢您的帮助。
编辑:
我现在已经尝试了另一种解决方案,该解决方案被证明更快但仍然不够快:
NSUInteger idx = [self.myMessagesTableView.selectedRowIndexes firstIndex];
NSMutableArray *selectedObjects = [NSMutableArray new];
while (idx != NSNotFound)
{
// do work with "idx"
// NSLog (@"The current index is %lu", (unsigned long)idx);
[selectedObjects addObject:self.myMessages[idx]];
// get the next index in the set
idx = [self.myMessagesTableView.selectedRowIndexes indexGreaterThanIndex:idx];
}
NSMutableArray *unselectedObjects = [NSMutableArray new];
[self.unFilteredItems enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![selectedObjects containsObject:obj]) {
[unselectedObjects addObject:obj];
}
}];
self.items = selectedObjects;
self.items = [self.items arrayByAddingObjectsFromArray:unselectedObjects];
[self.itemsTableView reloadData];
我希望有人可以指出正确的方向,可能是尝试使用选择索引而不是所选对象的数组。再次感谢。
答案 0 :(得分:1)
如何获取选定的对象并附加未选定的对象
NSArray *selectedObjects = [self.items objectsAtIndexes:self.itemsTableView.selectedRowIndexes];
// invert selectedRowIndexes
NSMutableIndexSet *invertedSelection = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.items.count)];
[invertedSelection removeIndexes:self.itemsTableView.selectedRowIndexes];
NSArray *unselectedObjects = [self.items objectsAtIndexes:invertedSelection];
self.items = [selectedObjects arrayByAddingObjectsFromArray:unselectedObjects];
self.itemsTableView.selectedRowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, selectedObjects.count)];
[self.itemsTableView reloadData];
或删除所选对象并将其插入顶部
NSMutableArray *mutableItems = [self.items mutableCopy];
NSArray *selectedObjects = [self.items objectsAtIndexes:self.itemsTableView.selectedRowIndexes];
[mutableItems removeObjectsAtIndexes:self.itemsTableView.selectedRowIndexes];
NSindexSet *selectedIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, selectedObjects.count)]
[mutableItems insertObjects:selectedObjects atIndexes:selectedIndexes];
self.items = [mutableItems copy];
self.itemsTableView.selectedRowIndexes = selectedIndexes;
[self.itemsTableView reloadData];
我没有测试。
答案 1 :(得分:0)
我发现了一种实际上可以非常快速地工作的方法,但是它并不优雅,肯定会破坏我认为的MVC模式,但是目前它可以工作,因此任何其他更好的解决方案都非常受欢迎。我现在正在使用此代码:
NSUInteger idx = [self.itemsTableView.selectedRowIndexes firstIndex];
NSMutableArray *selectedObjects = [NSMutableArray new];
while (idx != NSNotFound)
{
// do work with "idx"
// NSLog (@"The current index is %lu", (unsigned long)idx);
[self.items[idx] setValue:@YES forKey:@"isItemSelected"];
[selectedObjects addObject:self.items[idx]];
// get the next index in the set
idx = [self.itemsTableView.selectedRowIndexes indexGreaterThanIndex:idx];
}
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"isItemSelected" ascending:NO];
self.items = [self.unFilteredItems sortedArrayUsingDescriptors:@[descriptor]];
[self.itemsTableView reloadData];
NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
NSUInteger a = 0;
for (a=0;a<[selectedObjects count];a++) {
[set addIndex:a];
}
[self.itemsTableView selectRowIndexes:set byExtendingSelection:NO];
[selectedObjects enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeObjectForKey:@"isItemSelected"];
}];
非常感谢您的关注。