objective-c - 每次单击时扩展大纲视图的行时如何更新行值和子项
问题描述
我是目标 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;
}
}
现在的问题是:表格在每次点击后重新加载,但永远不会在行中添加新的子项来扩展它。相反,它会折叠所有项目。但是在再次单击后(相同的项目单击了两次),该项目会正确展开新的子项。我想要的是:重新加载表并在获得新值后用它的新子级扩展该行。
解决方案
我可以通过以下代码添加要查看的项目
[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];
推荐阅读
- flutter - 隐藏 SliverAppBar 时隐藏设备状态栏
- lilypond - 用 Lilypond 连接“[”条线
- swift - 我可以从 Xcode 自动完成中判断参数标签是否是可选的吗?
- database - Couchbase 5.1 N1QL OR 运算符不使用索引
- powershell - Powershell替换文本字符串
- api - Google Analytics API (v4) - 我怎样才能得到 pagePath 或 url 作为响应?目前只获取 pageTitle 和综合浏览量
- sql - 如何在 IN 语句中获取 20 万封电子邮件的信息而不会收到错误消息
- python - 拉取位于数组中的多个字符串的特定部分。
- django - 在Django中通过字符串更改模型属性
- java - 椭圆形移动程序。椭圆不动,有什么我可以在这里炼制的吗?