Drag and drop single cell in a table/matrix

  • Hi all,

    The short question is: how do I drag'n'drop single cells in an
    NSTableView (or NSOutlineView). I.e. I don't want the entire row to be
    dragged, just one cell.

    Is there any example code on how to do this? Or is there anyone who
    might push me in the right direction? Is
    tableView:shouldTrackCell:forTableColumn:row: something I should learn
    more about? Or is writeRowsWithIndexes what I want?

    I googled myself dizzy but haven't found an answer. The NSMatrix class
    looks promising, but I need different column widths as well as
    headers. The NSTableView also looks promising, but dragging seems very
    "row based".

    A little more information about the situation: I have a model which
    (simplified) looks like this: there is an Area entity and a Type
    entity and finally an Item entity. The Area and Type entities have a
    title property.

    Each Item belongs to exactly one Area and one Type

    Item <<----> Area
    Item <<----> Type

    There might or might not be an Item for a specific Area/Type
    combination.

    There is no (explicit) relation between Area and Type. I would like to
    present this in a "matrix" (NSTableView, NSMatrix, or ???) with Area-s
    going vertical and Type-s going horizontal. Like this (monospaced font
    probably required...):

      | Type1  | Type2  |
    ------+---------+--------+
    Area1 |  Item11 |        |
    ------+---------+--------+
    Area2 |        | Item22 |
    ------+---------+--------+
    Area3 |  Item31 | Item32 |
    ------+---------+--------+

    And now the tricky part, there might be a relation between two Item-s.
    This relation is defined by the user and I would like to implement
    this with drag and drop. Of course I could use a context menu, but
    drag and drop feels much more "mac-like" and elegant.

    So, for creating a relation between Item11 and, say, Item32, the user
    drag and drops the *cell* Item11 on the cell Item32.

    Thanks
    /Dan
  • Override:

    - (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows
    tableColumns:(NSArray *)tableColumns event:(NSEvent*)dragEvent offset:
    (NSPointPointer)dragImageOffset

    limit the columns passed based on the column at the event's location.
    The rest is up to your delegate/datasource methods.

    corbin

    On Feb 28, 2008, at 2:14 PM, Dan Waltin wrote:
    > Hi all,
    >
    > The short question is: how do I drag'n'drop single cells in an
    > NSTableView (or NSOutlineView). I.e. I don't want the entire row to
    > be dragged, just one cell.