gpt4 book ai didi

objective-c - 从大纲 View 中删除行

转载 作者:行者123 更新时间:2023-12-03 18:06:49 24 4
gpt4 key购买 nike

我有一个大纲 View 和一个按钮。我想要该按钮删除大纲 View 中选定的行。我已将大纲 View 的选择索引路径绑定(bind)到树 Controller ,将按钮连接到树 Controller 的 remove: 选择器,并将按钮绑定(bind)到树 Controller 的 canRemove: 属性。树 Controller 。但是,一旦我完成了所有这些(我希望能够工作),它就完全破坏了大纲 View ,当我说破坏时,我的意思是它不再工作,它不会显示我添加的任何数据。

现在,为了了解更多信息,这里有一些数组 Controller 和树 Controller 连接的图片。

NSArrayController 连接

alt text http://snapplr.com/snap/b5a1

NSTreeController 连接(如您所见,没有选择索引路径绑定(bind))

alt text http://snapplr.com/snap/qv62

最后一件事,大纲 View 有一个数据源,允许它进行拖放,我认为这与它不起作用有关,所以下面是数据源中的代码。

实现文件。

#import "DragController.h"


@implementation DragController
- (void)awakeFromNib {

dragType = [NSArray arrayWithObjects: @"factorialDragType", nil];

[ dragType retain ];

[ treeTable registerForDraggedTypes:dragType ];
NSSortDescriptor* sortDesc = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
[groupTreeControl setSortDescriptors:[NSArray arrayWithObject: sortDesc]];
[ sortDesc release ];
}


//------------------------------------
#pragma mark NSOutlineView datasource methods -- see NSOutlineViewDataSource
//---------------------------------------------------------------------------
- (BOOL) outlineView : (NSOutlineView *) outlineView
writeItems : (NSArray*) items
toPasteboard : (NSPasteboard*) pboard {

[ pboard declareTypes:dragType owner:self ];
// items is an array of _NSArrayControllerTreeNode see http://theocacao.com/document.page/130 for more info
draggedNode = [ items objectAtIndex:0 ];

return YES;
}




- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index {

_NSArrayControllerTreeNode* parentNode = item;
_NSArrayControllerTreeNode* siblingNode;
_NSControllerTreeProxy* proxy = [ groupTreeControl arrangedObjects ];

NSManagedObject* draggedGroup = [ draggedNode observedObject ];

BOOL draggingDown = NO;
BOOL isRootLevelDrag = NO;

// ----------------------
// Setup comparison paths
// -------------------------
NSIndexPath* draggedPath = [ draggedNode indexPath ];
NSIndexPath* siblingPath = [ NSIndexPath indexPathWithIndex: index ];
if ( parentNode == NULL ) {
isRootLevelDrag = YES;
} else {
// A non-root drag - the index value is relative to this parent's children
siblingPath = [ [ parentNode indexPath ] indexPathByAddingIndex: index ];
}

// ----------------------
// Compare paths - modify sibling path for down drags, exit for redundant drags
// -----------------------------------------------------------------------------
switch ( [ draggedPath compare:siblingPath] ) {
case NSOrderedAscending: // reset path for down dragging
if ( isRootLevelDrag ) {
siblingPath = [ NSIndexPath indexPathWithIndex: index - 1];
} else {
siblingPath = [ [ parentNode indexPath ] indexPathByAddingIndex: index - 1 ];
}
draggingDown = YES;
break;

case NSOrderedSame:
return NO;
break;
}

siblingNode = [ proxy nodeAtIndexPath:siblingPath ];

// NSLog(@"returning early");
// return NO; // TODO robustify


// ------------------------------------------------------------
// SPECIAL CASE: Dragging to the bottom
// ------------------------------------------------------------
// - K - K - C - C
// - - U - - C OR - U - F
// - - C ====> - - F - F - K
// - - F - U - K - U
// ------------------------------------------------------------
if ( isRootLevelDrag && siblingNode == NULL ) {
draggingDown = YES;
siblingPath = [ NSIndexPath indexPathWithIndex: [ proxy count ] - 1 ];
siblingNode = [ proxy nodeAtIndexPath:siblingPath ] ;
}

// ------------------------------------------------------------
// Give the dragged item a position relative to it's new sibling
// ------------------------------------------------------------
NSManagedObject* sibling = [ siblingNode observedObject ];
NSNumber* bystanderPosition = [ sibling valueForKey:@"position"];
int newPos = ( draggingDown ? [ bystanderPosition intValue ] + 1 : [ bystanderPosition intValue ] - 1 );
[draggedGroup setValue:[ NSNumber numberWithInt:newPos ] forKey:@"position"];

// ----------------------------------------------------------------------------------------------
// Set the new parent for the dragged item, resort the position attributes and refresh the tree
// ----------------------------------------------------------------------------------------------
[ draggedGroup setValue:[ parentNode observedObject ] forKey:@"parent" ];
[ self resortGroups:[draggedGroup managedObjectContext] forParent:[ parentNode observedObject ] ];
[ groupTreeControl rearrangeObjects ];
return YES;
}






- (NSArray* ) getSubGroups:(NSManagedObjectContext*)objectContext forParent:(NSManagedObject*)parent {
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"projects" inManagedObjectContext:objectContext];

[request setEntity:entity];
NSSortDescriptor* aSortDesc = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject: aSortDesc] ];
[aSortDesc release];

NSPredicate* validationPredicate = [NSPredicate predicateWithFormat:@"parent == %@", parent ];

[ request setPredicate:validationPredicate ];

NSError *error = nil; // TODO - check the error bozo
return [objectContext executeFetchRequest:request error:&error];
}




- (void) resortGroups:(NSManagedObjectContext*)objectContext forParent:(NSManagedObject*)parent {

NSArray *array = [ self getSubGroups:objectContext forParent:parent ];

// Reset the indexes...
NSEnumerator *enumerator = [array objectEnumerator];
NSManagedObject* anObject;
int index = 0;
while (anObject = [enumerator nextObject]) {
// Multiply index by 10 to make dragging code easier to implement ;) ....
[anObject setValue:[ NSNumber numberWithInt:(index * INTERVAL ) ] forKey:@"position"];
index++;
}


}

- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index {

_NSArrayControllerTreeNode* newParent = item;

// drags to the root are always acceptable
if ( newParent == NULL ) {
return NSDragOperationGeneric;
}

// Verify that we are not dragging a parent to one of it's ancestors
// causes a parent loop where a group of nodes point to each other and disappear
// from the control
NSManagedObject* dragged = [ draggedNode observedObject ];
NSManagedObject* newP = [ newParent observedObject ];

if ( [ self category:dragged isSubCategoryOf:newP ] ) {
return NO;
}

return NSDragOperationGeneric;
}

- (BOOL) category:(NSManagedObject* )cat isSubCategoryOf:(NSManagedObject* ) possibleSub {

// Depends on your interpretation of subCategory ....
if ( cat == possibleSub ) { return YES; }

NSManagedObject* possSubParent = [possibleSub valueForKey:@"parent"];

if ( possSubParent == NULL ) { return NO; }

while ( possSubParent != NULL ) {
if ( possSubParent == cat ) { return YES; }

// move up the tree
possSubParent = [possSubParent valueForKey:@"parent"];
}

return NO;
}




// This method gets called by the framework but the values from bindings are used instead
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return NULL;
}

/*
The following are implemented as stubs because they are required when
implementing an NSOutlineViewDataSource. Because we use bindings on the
table column these methods are never called. The NSLog statements have been
included to prove that these methods are not called.
*/
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
NSLog(@"numberOfChildrenOfItem");
return 1;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
NSLog(@"isItemExpandable");
return NO;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
NSLog(@"child of Item");
return NULL;
}



@end

最佳答案

我已经弄清楚了,所以我将答案发布出来,以帮助所有可能有同样问题的人。要修复它,只需执行此操作。

  1. 将 NSTreeController 的 managedObjectContext 直接绑定(bind)到 AppDelegate 的 ManagedObjectContext 属性,并删除整个 NSArrayController。 NSTreeController 可以从 managedObjectContext 添加/删除项目,但不能从 NSArrayController 的排列对象添加/删除项目,因为它是只读的。

  2. 现在将添加按钮连接到树 Controller 的 add: 连接。

  3. 最后将删除按钮连接到树 Controller 的remove:连接。

关于objective-c - 从大纲 View 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1198381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com