首页 > 解决方案 > 每次单击时扩展大纲视图的行时如何更新行值和子项

问题描述

我是目标 c 的新手。我一直在寻找描述项目大纲视图扩展的示例/答案。现在我要做的是,将所有项目标记为可扩展。现在,当用户单击展开项目时,它会调用 API,插入其子项(如果有),然后重新加载表格视图。代码如下:

 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item{
//initially make all items expandable to check and load data later on
            return YES;
    }

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item{    
    if (item == nil) {     
        return RootLists.count;
    }
    if ([item isKindOfClass:[NSDictionary class]]) {
        reloadCheck=true;
        return [[item objectForKey:@"children"] count];
    }
    return 0;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item{

    if (item == nil){       
        return [RootLists objectAtIndex:index];
    }
    if ([item isKindOfClass:[NSDictionary class]]) {
        return [[item objectForKey:@"children"] objectAtIndex:index];
    }
    return nil;
}


-(NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item{
    NSView *rowView;
    rowView=[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
    [rowView setWantsLayer:YES];   
     NSTextField *textField;        
    [textField setMaximumNumberOfLines:1];
    [textField setStringValue:[item valueForKey:@"Name"]];       
    [textField setBezeled:NO];
    [textField setDrawsBackground:NO];
    [textField setEditable:NO];
    [textField setSelectable:NO];  
    [rowView addSubview:textField];
    return rowView;
}



    - (BOOL)outlineView:(NSOutlineView *)outlineView  shouldExpandItem:(id)item{
                NSMutableArray *getchildrenlist=[database GetData:[item valueForKey:@"id"]];    
                if (getchildrenlist.count>0) {
//If children are already present then return 1
        return 1;
                }
                else
                {
//If children are not present then hit API, get children online and then reload the view with children
                    dispatch_group_t DispatchGroup = dispatch_group_create();
                    dispatch_group_enter(DispatchGroup);
                    [self ExpandDataApi:1 FolderId:[item valueForKey:@"id"] DispatchGroup:DispatchGroup];              
                    dispatch_group_notify(DispatchGroup,dispatch_get_main_queue(),^{
                                [self reloadData:item];
                                [self->_Outline_View reloadData];
    });
        return 1;
                }

            }

现在的问题是:表格在每次点击后重新加载,但永远不会在行中添加新的子项来扩展它。相反,它会折叠所有项目。但是在再次单击后(相同的项目单击了两次),该项目会正确展开新的子项。我想要的是:重新加载表并在获得新值后用它的新子级扩展该行。

标签: objective-cmacoscocoa

解决方案


我可以通过以下代码添加要查看的项目

[self->_Outline_View insertItemsAtIndexes:[NSIndexSet indexSetWithIndex:row] inParent:item withAnimation:NSTableViewAnimationSlideDown];

并按代码从行中删除项目:

  [self->_Outline_View removeItemsAtIndexes:[NSIndexSet indexSetWithIndex:row] inParent:item withAnimation:NSTableViewAnimationSlideDown];

然后使用代码刷新它:

[self->_Outline_View reloadItem:item];
  [self->_Outline_View expandItem:item];

推荐阅读