每次单击大纲视图行时,如何更新行值和子项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每次单击大纲视图行时,如何更新行值和子项相关的知识,希望对你有一定的参考价值。

我是客观的新人。我一直在寻找描述项目大纲视图扩展的示例/答案。现在我要做的是,将所有项目标记为可扩展。现在,当用户单击以展开项时,它会调用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;
                }

            }

现在的问题是:每次单击后表都会重新加载,但是从不扩展它,并将新子项添加到行中。相反,它会折叠所有项目。但在再次单击(同一项目单击两次)后,该项目将与新子项正确扩展。我想要的是:重新加载表并在获取新值后使用新子项扩展该行。

答案

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

[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];

以上是关于每次单击大纲视图行时,如何更新行值和子项的主要内容,如果未能解决你的问题,请参考以下文章

单击表格行时更改 UISlider 控制器

单击表格视图行时如何推回视图?

如果在动画正在进行时单击,Android Fragment 事务动画会崩溃

如果您使用 pandas 数据框知道列值和行值,如何检索数据? [复制]

如何将可扩展列表视图的选定子视图数据从片段发送到父活动?

单击或选择 Clistctrl 子项(报告视图)