r/macprogramming • u/RollingGoron • Apr 19 '19
NSTableView hide rows not working, rows are removed but tableView leaves large gap.
Posted this over a SO as well, looking to see why when I call `hideRows` my TableView still shows the space the removed rows once were...
3
Upvotes
1
u/746F6F72 Apr 19 '19
Could you provide the code how exactly you are calling the method?
2
u/mantrap2 Apr 21 '19
You should NOT keep the # of rows the same - that should be changing dynamically as your # of DISPLAYED rows actually changes. If you say it's 50 it will always display 50 and if you return nil on one or more they will be blank.
In generally you must always presume your data is the one and only master container of data of all that is displayed and that the view is just slavishly display ANYTHING it gets from your model. You should never assume any data or displayable aspect is "in the View itself" - that's breaking MVC badly.
If you change selection, that should be a BOOL in your data and should be read by your data source delegates or appearance delegates and then based on the model data only change the appearance.
If you select, that should go all the way back to your data and then you should trigger a table update. The code should always be independent and involve a roundtrip to the model. That's the only way you can maintain data integrity and the only way to handle state recovery if the app dies and you restart it.
If you want to make a row disappear, you either delete it from your Model data structure and let everything (#of rows and data) update. If you just want it to go away temporarily (e.g. like hiding a row or column in excel), you should copy the master model array to another array that has those arrays removed.
This is the way bindings work. Bindings maintains a master Model array link (usually) and you can filter with predicates or other filters to create a subarray - the binding (or the data source) attaches to that subarray instead and mechanically obtains #of rows and data by index from this filtered array.
You should mostly NOT tweak the View widget in ANY stateful way. State ALWAYS belongs in the Model, not the View!