Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将焦点移动到NSTableView中新添加的记录_Objective C_Cocoa_Macos_Nstableview - Fatal编程技术网

Objective c 将焦点移动到NSTableView中新添加的记录

Objective c 将焦点移动到NSTableView中新添加的记录,objective-c,cocoa,macos,nstableview,Objective C,Cocoa,Macos,Nstableview,我正在编写一个应用程序,使用核心数据来控制几个NStableView。我有一个add按钮,它在NSTableView中创建一个新记录。单击此按钮时,如何使焦点移动到新记录,以便立即键入其名称?这与iTunes中的想法相同,单击“添加播放列表”按钮后,键盘焦点会立即移动到新行,以便您可以键入播放列表的名称。好的,首先,如果您还没有,您需要为应用程序创建一个控制器类。在控制器类的界面中,为存储对象的NSArrayController添加一个出口,并为显示对象的NSTableView添加一个出口 IB

我正在编写一个应用程序,使用核心数据来控制几个NStableView。我有一个add按钮,它在NSTableView中创建一个新记录。单击此按钮时,如何使焦点移动到新记录,以便立即键入其名称?这与iTunes中的想法相同,单击“添加播放列表”按钮后,键盘焦点会立即移动到新行,以便您可以键入播放列表的名称。

好的,首先,如果您还没有,您需要为应用程序创建一个控制器类。在控制器类的界面中,为存储对象的
NSArrayController
添加一个出口,并为显示对象的
NSTableView
添加一个出口

IBOutlet NSArrayController *arrayController;
IBOutlet NSTableView *tableView;
将这些插座连接到IB中的
NSArrayController
NSTableView
。然后需要创建一个
iAction
方法,当按下“添加”按钮时调用该方法;将其称为
addButtonPressed:
或类似名称,在控制器类界面中声明:

- (IBAction)addButtonPressed:(id)sender;
并使其成为IB中“添加”按钮的目标

现在需要在控制器类的实现中实现此操作;此代码假定已添加到阵列控制器的对象是
NSString
s;如果不是,则将
new
变量的类型替换为要添加的任何对象类型

//Code is an adaptation of an excerpt from "Cocoa Programming for
//Mac OS X" by Aaron Hillegass
- (IBAction)addButtonPressed:(id)sender
{
//Try to end any editing that is taking place in the table view
NSWindow *w = [tableView window];
BOOL endEdit = [w makeFirstResponder:w];
if(!endEdit)
  return;

//Create a new object to add to your NSTableView; replace NSString with
//whatever type the objects in your array controller are
NSString *new = [arrayController newObject];

//Add the object to your array controller
[arrayController addObject:new];
[new release];

//Rearrange the objects if there is a sort on any of the columns
[arrayController rearrangeObjects];

//Retrieve an array of the objects in your array controller and calculate
//which row your new object is in
NSArray *array = [arrayController arrangedObjects];
NSUInteger row = [array indexOfObjectIdenticalTo:new];

//Begin editing of the cell containing the new object
[tableView editColumn:0 row:row withEvent:nil select:YES];
}

然后,当您单击“添加”按钮时,将调用该命令,并且新行第一列中的单元格将开始编辑。

我认为一种更简单、更合适的方法是通过这种方式实现它

-(void)tableViewSelectionDidChange:(NSNotification *)notification {
    NSLog(@"%s",__PRETTY_FUNCTION__);
    NSTableView *tableView = [notification object];
    NSInteger selectedRowIndex = [tableView selectedRow];
    NSLog(@"%ld selected row", selectedRowIndex);

    [tableView editColumn:0 row:selectedRowIndex withEvent:nil select:YES];

  • 实施
    tableViewSelectionDidChange:(NSNotification*)通知
  • 获取选定的行索引
  • 调用
    editColumn:(NSInteger)column row:(NSInteger)row with event:(NSEvent*)事件选择:(BOOL)使用行索引从那里选择

  • 重要提示:当用户只需选择一行时,此解决方案还将触发编辑。如果您只希望在添加新对象时触发编辑,则这不适合您。

    只需在控制器中创建一个单独的
    @IBAction
    ,并手动调用
    NSArrayController.add
    方法。之后,您可以选择该行

    @IBAction func addLink(_ sender: Any) {
        // Get the current row count from your data source
        let row = links.count
    
        arrayController.add(sender)
    
        DispatchQueue.main.async {
            self.tableView.editColumn(0, row: row, with: nil, select: true)
        }
    }
    

    您的数据是如何存储的?在阵列控制器中?是的,它在NSArrayController中。NSArrayController具有iAction“add”。您可以将按钮链接到此操作。你能直接打电话吗?(我试过了,不起作用)如果可以的话,你必须创建一个像这样的新对象,但只需调用add,对吗?