Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update NSTableView without using -reloadData?

I am new to the Mac. I am trying to update a particular cell in NSTableView without using -reloadData, as -reloadData updates the whole table. I have tried everything but all was in vain. I am trying to do something similar to what we used to do in CListCtrl in MFC or in .NET.

like image 783
Omayr Avatar asked Oct 20 '10 11:10

Omayr


2 Answers

Have a look at reloadDataForRowIndexes:columnIndexes: method. To update a single cell the following should work:

[yourTable reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row]  
                     columnIndexes:[NSIndexSet indexSetWithIndex:column]];
like image 114
Vladimir Avatar answered Oct 07 '22 09:10

Vladimir


This is a fundamental difference between the way views typically work in Cocoa and how they work in some other frameworks. NSTableView does expose a -reloadDataForRowIndexes:columnIndexes: API, as Vladimir points out, but you'll notice the absence of a "withObject:" parameter.

This is because Cocoa views and controls are not designed to also act as data containers. Rather, they're for presentation of and interaction with data that is managed by some other model object, usually through an intermediate controller of some sort.

Given a table view, for example, if your model has informed your controller that some particular data has changed, the controller can invalidate the displayed portion of the table that maps to that value - if there even is a displayed portion of the table. The next time it draws itself, the table will ask its data source (the controller) for the values to present for the area it needs to redraw.

Thus despite a superficial similarity in user interaction, you'll probably want to reconsider how you write the code to implement your user interface. You'll wind up with much more factored Model-View-Controller style code in the end; the price is that you'll need to actually represent your model more thoroughly than you might have had to in frameworks that didn't require such a separation of concerns.

like image 26
Chris Hanson Avatar answered Oct 07 '22 08:10

Chris Hanson