如何使 NSTableViewRow 内的按钮响应所表示的对象

lar*_*rod 5 cocoa nstableview cocoa-bindings nstablecellview

我已经和这个问题斗争了一段时间了。我正在开发文件复制管理器模块,到目前为止,除了取消按钮之外,我已经能够使一切正常工作。由于某种原因,当我单击特定行的取消按钮时,按钮操作同时针对多行。

文件复制管理器

经过几天的研究这个问题后,我能够使用以下命令使对象成功取消该行所代表的操作:

-(IBAction)btnCancelOperationClick:(id)sender {
    NSInteger row = [_tableView rowForView:sender];
    if (row != -1) {
        FileCopyOperation *opr = [_fileCopyOperations objectAtIndex:row];
        [opr cancel];
        [_fileCopyOperations removeObjectAtIndex:row];
        [_tableView removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:row] withAnimation:NSTableViewAnimationEffectFade];
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用,我可以取消操作并相应地更新我的表。其他一切都按预期工作,但我的代码或绑定一定有问题。我从笔尖加载此单元格,然后使用以下方法注册此笔尖:

[_tableView registerNib:[[NSNib alloc]initWithNibNamed:@"FileCopyCell" bundle:nil] forIdentifier:@"FileCopyCell"];
Run Code Online (Sandbox Code Playgroud)

我将 QueueController 设置为文件的所有者,并像这样挂钩所有内容:

绑定

如果有人能为我指出正确的方向以使这件事正常工作,我将不胜感激。提前致谢!

编辑以添加更多代码示例。

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    FileCopyCell *cell = [tableView makeViewWithIdentifier:@"FileCopyCell" owner:self];
    FileCopyOperation *opr = [_fileCopyOperations objectAtIndex:row];

    [cell.fileName setStringValue:[NSString stringWithFormat:@"Copying \"%@\"",opr.fName]];
    [cell.progressBar setDoubleValue:((opr.bWritten.doubleValue / opr.fSize.doubleValue) * 100)];
    [cell.totalBytes setStringValue:[NSString stringWithFormat:@"of %@",[NSByteCountFormatter stringFromByteCount:opr.fSize.longLongValue countStyle:NSByteCountFormatterCountStyleFile]]];
    [cell.status setStringValue:[NSString stringWithFormat:@"%@",[NSByteCountFormatter stringFromByteCount:opr.bWritten.longLongValue countStyle:NSByteCountFormatterCountStyleFile]]];
    [cell.icon setImage:[[NSWorkspace sharedWorkspace]iconForFile:opr.srcURL.path]];
    [cell.cancelButton setTarget:self];
    return cell;
}

-(void)windowDidLoad {
    [super windowDidLoad];
    _fileCopyOperations = [NSMutableArray new];
    windowFrame = [self.window frame];
    rows = 0;

    [_tableView registerNib:[[NSNib alloc]initWithNibNamed:@"FileCopyCell" bundle:nil] forIdentifier:@"FileCopyCell"];

    if (!fileCopyManager) {
        fileCopyManager = [FileCopyManager sharedFileCopyManager];
        [fileCopyManager.fileCopyQueue addObserver:self forKeyPath:@"operationCount" options:NSKeyValueObservingOptionNew context:(void*)fileCopyManager];
    }

    [_scrollView setHasHorizontalScroller:NO];
    [_scrollView setHasVerticalScroller:NO];
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*zzi 5

最好的方法是子类化NSTableCellView并让它处理自己的操作和表示的对象。例如,表示Foo实例的单元格可以具有两个属性:foofooController。当调用 ( nonatomic) foosetter 时,单元格可以更新自己的 UI 来表示传递的Foo. 当Foo控制器创建表格单元格时,它可以实例化一个FooCell实例,将其自身设置为fooController并分配该Foo实例,然后让单元格自行处理。取消按钮的目标可以是它自己的单元格,当-cancel:调用该操作时,它可以告诉它fooController要做什么(因为Foo控制器负责更新队列和表视图),并且由于它有对其的引用foo,所以它可以通过某种方法将其传递给控制器​​,-cancelFoo:(Foo *)theFoo而不依赖于控制器查找其索引(如果您要对行的出现和消失进行动画处理,或者用户快速连续取消一堆,但它们的删除被延迟,则这可能不准确)异步更新)。

漂亮又干净。整齐有序的遏制/职责分离。该单元处理自己的 UI 更新和操作,并知道自己的 foo;foo 控制器处理它的 foo 集合、它的表视图以及 foo 到 foo 单元格的分配。