- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当在 NSOutlineView 中创建新项目时,我希望选择该项目并可编辑,我当前在 NSOutlineview 委托(delegate)/数据源中使用以下代码,创建了新项目,但它不是重点/可编辑。
NSWindow *w = [[self outlineView] window];
BOOL endEdit = [w makeFirstResponder:w];
if (!endEdit) {
return;
}
NSUInteger row = [[self outlineView] rowForItem:newGoal];
[[self outlineView] scrollRowToVisible:row];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:row];
[[self outlineView] selectRowIndexes:indexSet byExtendingSelection:NO];
[[self outlineView] editColumn:0
row:row
withEvent:nil
select:YES];
NSError *anyError = nil;
BOOL success = [[self myContext] save:&anyError];
if (!success) {
NSLog(@"Error = %@", anyError);
}
我的完整文件是
//
// SBOutlineViewController.m
// Allocator
//
// Created by Cory Sullivan on 2013-04-21.
//
//
#import "SBOutlineViewController.h"
#import "SBAppDelegate.h"
@interface SBOutlineViewController ()
@end
NSString * const SBOutlineSelectionChangedNotification = @"SBSelectionChanged";
@implementation SBOutlineViewController
- (void)awakeFromNib
{
[self setTopLevelItems:[NSArray arrayWithObjects:@"Retirement", @"Education", nil]];
[self setMyContext:[[NSApp delegate] managedObjectContext]];
[self updateOutlineView];
}
- (void)updateOutlineView
{
NSFetchRequest *myRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *goalEntity = [NSEntityDescription entityForName:@"Goal" inManagedObjectContext:[self myContext]];
[myRequest setEntity:goalEntity];
NSError *anyError = nil;
[self setGoalItems:[[self myContext] executeFetchRequest:myRequest error:&anyError]];
NSMutableArray *retirementArray = [[NSMutableArray alloc] init];
NSMutableArray *educationArray = [[NSMutableArray alloc] init];
for (NSManagedObject *goalObject in [self goalItems]) {
if ([[goalObject valueForKey:@"goalType"] isEqual: @"Retirement"]) {
[retirementArray addObject:goalObject];
} else {
if ([[goalObject valueForKey:@"goalType"] isEqual:@"Education"]) {
[educationArray addObject:goalObject];
}
}
}
[self setMyOutlineDictionary:[[NSMutableDictionary alloc] initWithObjectsAndKeys:retirementArray, @"Retirement", educationArray, @"Education", nil]];
[[self outlineView] reloadData];
}
#pragma mark - Delegate and DataSoure Methods
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return [[self _childrenForItem:item] objectAtIndex:index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if ([outlineView parentForItem:item] == nil) {
return YES;
} else {
return NO;
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
if ([outlineView parentForItem:item] == nil) {
return NO;
} else {
return YES;
}
}
- (NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return [[self _childrenForItem:item] count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return [_topLevelItems containsObject:item];
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
if ([_outlineView selectedRow] != -1) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:SBOutlineSelectionChangedNotification object:self];
}
}
- (NSArray *)_childrenForItem:(id)item {
NSArray *children;
if (item == nil) {
children = _topLevelItems;
} else {
children = [_myOutlineDictionary objectForKey:item];
}
return children;
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
NSTableCellView *view = nil;
if ([_topLevelItems containsObject:item]) {
view = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
NSString *value = [item uppercaseString];
[[view textField] setStringValue:value];
return view;
} else {
view = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
[[view imageView] setImage:[NSImage imageNamed:NSImageNameActionTemplate]];
NSManagedObject *goalItem = [_goalItems objectAtIndex:[_goalItems indexOfObject:item]];
[[view textField] setStringValue:[goalItem valueForKey:@"goalName"]];
return view;
}
}
#pragma mark - Custom Actions
- (IBAction)goalActionPullDown:(id)sender
{
[self modifyGoalItems: [[sender selectedItem] tag]];
}
- (void)modifyGoalItems:(NSInteger)whichMenuTag
{
if (whichMenuTag == 5) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Are you sure you want to delete goal and all related accounts?"
defaultButton:@"Remove"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"This can't be undone"];
[alert beginSheetModalForWindow:[_outlineView window]
modalDelegate:self
didEndSelector:@selector(alertEnded:code:context:)
contextInfo:NULL];
} else {
NSManagedObject *newGoal = [NSEntityDescription insertNewObjectForEntityForName:@"Goal" inManagedObjectContext:[self myContext]];
switch (whichMenuTag) {
case 1:
{
[newGoal setValue:@"New Goal" forKey:@"goalName"];
[newGoal setValue:@"Retirement" forKey:@"goalType"];
[self updateOutlineView];
}
break;
case 2:
{
[newGoal setValue:@"New Goal" forKey:@"goalName"];
[newGoal setValue:@"Education" forKey:@"goalType"];
[self updateOutlineView];
}
break;
case 3:
NSLog(@"You selected Item number 3");
break;
case 4:
NSLog(@"You selected Item number 4");
break;
default:
break;
}
NSWindow *w = [[self outlineView] window];
BOOL endEdit = [w makeFirstResponder:w];
if (!endEdit) {
return;
}
NSUInteger row = [[self outlineView] rowForItem:newGoal];
[[self outlineView] scrollRowToVisible:row];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:row];
[[self outlineView] selectRowIndexes:indexSet byExtendingSelection:NO];
[[self outlineView] editColumn:0
row:row
withEvent:nil
select:YES];
NSError *anyError = nil;
BOOL success = [[self myContext] save:&anyError];
if (!success) {
NSLog(@"Error = %@", anyError);
}
}
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return YES;
}
- (void)alertEnded:(NSAlert *)alert
code:(int)choice
context:(void *)v
{
if (choice == NSAlertDefaultReturn) {
NSManagedObject *selectedGoal = [[self outlineView] itemAtRow:[[self outlineView] selectedRow]];
[[self myContext] deleteObject:selectedGoal];
NSError *anyError = nil;
BOOL success = [[self myContext] save:&anyError];
if (!success) {
NSLog(@"Error = %@", anyError);
}
[self updateOutlineView];
}
}
@end
最佳答案
任何尝试使该字段在当前运行循环中可编辑的尝试都将失败。您需要做的就是为下一次循环提示选择。像这样的事情:
[self performSelector:@selector(selectText:) withObject:textField afterDelay:0];
您可能想在文本字段上执行 selectText 以外的其他操作,但技术正是您所需要的。
关于cocoa - 为 NSOutlineView 制作新条目,重点关注且可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16649429/
根据Apple's documentation , NSOutlineView 要求 View 中的每个项目都是唯一的,但是,如果这实际上不符合业务需求怎么办?特别是,如何显示这样的内容 Item A
我有一个基于 View 的 NSOutlineView,我正在努力使项目可编辑。如果我单击一个项目来编辑它,它将从我单击的插入点开始,而不是选择整个文本。按 Enter/Return 进行编辑会选择整
我有一个带有复选框的 NSOutlineView。我将复选框状态绑定(bind)到带有键 shouldBeCopied 的节点项。在节点项中,我有 getter 和 setter,如下所示: -(BO
获得一个在 Controller 的 awakeFromNib 中配置的 NSOutlineView,带有数据源和自动保存的扩展节点。 一切都像魅力一样工作。 问题是在某个地方,有人也在保存滚动位置的
我阅读了关于 Outlineviews 和 DND 的另一篇文章,但我无法让我的程序运行。这篇文章的底部是我的项目 zip 的链接。它非常基本,只有一个大纲 View 和按钮。我希望它接收放置在其上的
我正在尝试将项目的实际折叠/展开状态存储在 NSOutlineView 中,以便稍后可以恢复。 NSOutlineViewDelegate 上有两种方法可用: outlineViewItemDidEx
我最近在没有帐户的情况下启动了另一个线程,因此我使用帐户在此处重新发布问题,以便我可以编辑该程序的当前链接,以便其他用户可以关注此问题。我还更新了下面的代码。这是我原来的问题: 我阅读了关于 Outl
看起来我的数据模型有一些问题,所以我时不时会收到此错误展开项目时。有谁知道这个断言意味着什么? 2010-10-20 03:04:47.547 test_tree[60332:613] * -NSOu
我正在尝试创建一个简单的文件夹浏览器,使用 NSOutlineView 显示树结构。将 monoMac 和 Xamarin Studio 与 XCode 界面生成器一起用于 UI。 我现在遇到的问题是
我有一个用 C# 编写的 MonoMac/Xamarin.MAc 项目。在其中,我有一个派生自 NSOutlineView 的自定义类,它显示某些项目的上下文菜单。通过这样做,而不是仅仅分配 Menu
我有一个简单的 NSOutlineView,通过 swift 初始化,有 2 列。我制作了以下非常简单的数据源,希望通过这种方式进行测试,但也许我这种格式是不允许的。我假设 TableView 仅根据
我想显示一个简单的 nsoutlineview 来显示层次结构。 当我通过 InterfaceBuilder 完成此操作时,一切都很好,但现在我尝试以编程方式进行操作,却遇到了一些麻烦。 这里是我用来
我正在开发一个应用程序,该应用程序需要一个源列表,例如在 Finder 中找到的源列表。到目前为止,我已经让核心数据与 NSOutlineView 一起使用,但组标题看起来不太像源列表。真实的源列表组
我想获取/设置NSOutlineView的选择索引路径。我该怎么做? 没有明显的方法来获取/设置选择索引路径。 如果您尝试绑定(bind)选择索引路径,并且没有将它们绑定(bind)到树 Contro
我创建了一个基于 View 的 NSOutlineView ,其中包含图像和徽章(内联按钮)。虽然自定义单元格足够宽,但我遇到以下问题: 因此,右侧有一个奇怪的边距覆盖了我的徽章(NSOutlineV
我有一个 NSOutlineView,上面有几个项目。每人有 3 个 child (地点、照片和社交)。他们都有相同的 child 。假设用户选择了地点,有谁知道我如何检测哪个项目是所选项目的父项?
我对此感到很难过。当涉及到基于 NSOutlineView 的 View 的对象持久性时,我遇到了非常奇怪的行为。目前我有一个对象,其中包含一个 object1 数组,该对象包含一个 object2
我正在使用基于 View 的 NSOutlineView 来创建源列表。NSOutlineView 连接到数据源和委托(delegate)对象。 但是,我似乎找不到需要实现的内容才能利用部分标题(例如
我正在尝试实现一个带有渐变背景的 NSOutlineView 子类: - (void)drawBackgroundInClipRect:(NSRect)clipRect { [gradient
我遇到了一个非常奇怪的问题: 我将 NSOutlineView 设置为 NSTableViewSelectionHighlightStyleSourceList。在某些情况下,第二级不缩进并与根节点显
我是一名优秀的程序员,十分优秀!