私はかなり長い間この問題と戦ってきました。私はファイル コピー マネージャー モジュールに取り組んでいます。これまでのところ、キャンセル ボタンを除いてすべてを完全に機能させることができました。何らかの理由で、特定の行のキャンセル ボタンをクリックすると、ボタン アクションが複数の行を同時にターゲットにします。
問題を数日間調査した後、次を使用して、行で表される操作をオブジェクトにキャンセルさせることができました。
-(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];
}
}
これはうまく機能します。操作をキャンセルして、それに応じてテーブルを更新できます。他のすべては意図したとおりに機能しますが、コードまたはバインディングに何か問題があるはずです。このセルを nib からロードし、次を使用してこの nib を登録します。
[_tableView registerNib:[[NSNib alloc]initWithNibNamed:@"FileCopyCell" bundle:nil] forIdentifier:@"FileCopyCell"];
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];
}