拖放NSTableView的自定义TableViewCell

时间:2017-07-05 09:46:55

标签: objective-c cocoa drag-and-drop nstableview nstableviewcell

    #define MyPrivateTableViewDataType @"MyPrivateTableViewDataType"

    //add line in your awakeFormNib method
    [self.tableView registerForDraggedTypes:[NSArray arrayWithObject:MyPrivateTableViewDataType]

// This method is called
     -(BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {
        // Copy the row numbers to the pasteboard.
        NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];

        [pboard declareTypes:[NSArray arrayWithObject:MyPrivateTableViewDataType] owner:self];

        [pboard setData:zNSIndexSetData forType:MyPrivateTableViewDataType];

        return YES;
    }

  ***following methods are not called***


    - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id )info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op {
        // Add code here to validate the drop
        return NSDragOperationEvery;
    }

    - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id )info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation {

        NSPasteboard* pboard = [info draggingPasteboard];
        NSData* rowData = [pboard dataForType:MyPrivateTableViewDataType];
        NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
        NSInteger dragRow = [rowIndexes firstIndex];

        if (dragRow < row) {
            [model insertObject:[model objectAtIndex:dragRow] atIndex:row];
            [model removeObjectAtIndex:dragRow];

            [aTableView noteNumberOfRowsChanged];
            [aTableView moveRowAtIndex:dragRow toIndex:row-1];

        }else {
            ModelObj *obj = [model objectAtIndex:dragRow];
            [model removeObjectAtIndex:dragRow];
            [model insertObject:obj atIndex:row];

            [aTableView noteNumberOfRowsChanged];
            [aTableView moveRowAtIndex:dragRow toIndex:row];
        }

        return YES;
   }

我的NSTableView有自定义tableViewCell,我注册了DraggedTypes。我设置表的委托和数据源。但是在3种方法中,只调用第一种方法-(BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard。 为什么不调用其他方法。

0 个答案:

没有答案