Displaying row index in an NSTableView bound to NSArrayController

后端 未结 3 1289
一生所求
一生所求 2021-02-09 13:18

I have an NSTableView which is bound to an NSArrayController. I would like to have one of the table columns showing the index of the table row. This is easy enough to do when yo

3条回答
  •  醉话见心
    2021-02-09 13:59

    Assuming that you aim to replicate the behavior of iTunes, your column only needs to display 1 to (number of visible rows). It does not actually need to relate to your model at all.

    So, give your controller a dynamically-generated array property, and make it depend on the array of actual model objects.

    For this property, implement the array accessor methods, with the indexed getter simply computing idx + 1, boxing it up, and returning that object. You may also need to implement the whole-array getter to satisfy KVC.

    Make sure the column is set as non-editable, and the binding set to not conditionally set enabled. Otherwise, you'll get an exception (for not having setters for this property) when the user tries to enter a new index for a row.

    One note of caution: This may cause problems, as NSTableView doesn't expect its columns to be bound to discrete arrays; it expects all of its columns to be bound to different properties of the model objects in a single array. You may need to bind the content of the table view itself to your array of model objects, in addition to binding the content bindings of the columns, and I've had trouble with that before.

提交回复
热议问题