NSTableView, multiple cells and bindings - objective-c

I'm trying to create a view that's similar to Motion's properties views.
Each of my property objects contains a definition of the kind of cell it wants to display as. But at the same time, I'd like to use bindings so that values are automatically updated as they can be changed elsewhere.
I've tried a few different approaches to the problem.
Multiple cells and dataCellForTableColumn: while this allows rendering to happen properly for all cell types, I lose bindings.
NSProxy: I've also tried using a proxy object that I thought would forward all methods to the selected cell type behind it, but again, bindings don't seem to work here.
Has anybody had any experience with this kind of problem before? Or is this one of the cases where bindings isn't going to cut it, and I'll need to do the heavy lifting myself?
Cheers!

I haven't actually tested it, but you shouldn't be losing bindings when you use dataCellForTableColumn:row: ? Can you describe in a bit more detail what you've tried with respect to bindings?
You should be able to set the column's value binding to the "value" property of your object. Then if you return the correct type of cell in dataCellForTableColumn:row: it should display correctly.
Edit: --- deleted gratuitous incorrect advice about cells vs views :)

Related

What is the best way to work with C++ model and TableView?

I rather newcomer in QML and can't find the best solution to work with QML TableView regarding C++ models. Almost all google's links say that I should use QAbstractTableModel. However, the problem in using this model (or better to say Presenter) is that I shoud emit dataUpdated every time anything is changed in real data.
But my approach is to create RowPresenter with QObject-derived properties for every column-cell (e.g. NameCellPresenter, StatisticsCellPresenter etc.) All these cell objects have complex data and it should be displayed in cell via properties. Moreover, I could display several types of data in one cell. And nobody, except these cell presenter objects, will never know about changing (RowPresenter or List of RowPresenter will only do their function of creating/removing...)
The questions are:
How could I efficiently update to QML the number of rows and rows from QList<RowPresenter> (without resetting ContextProperty) ?
Are there any methods to work with QAbstractTableModel in such way: only cell presenter knows about changing, and displaying complex data in one cell that could be changed independent from different threads?

How do you know what methods to use for a task?

I am learning Objective-C Cocoa programming for OS X, and object-based programming in general, so I am a big novice here, so my question is a bit general and my guess is the answer to this is simply "experience"; however, I am curious if there is some route of knowledge to understanding what methods in various classes are best or perhaps required for getting tasks done.
For example, in a programming guide I am instructed to create a document-based program, and the document class contains an array to store data, with the following method bound to a button to create a new entry in the array:
- (IBAction)insertItem:(id)sender {
if (!theItems) {
theItems = [NSMutableArray array];
}
[theItems addObject:#"Double-click to edit."];
[theTableView reloadData];
[self updateChangeCount:NSChangeDone];
}
The array is "theItems" and its data is being presented in a TableView object. I understand that the steps here add a new string to the array and then refresh the table to display it, followed by setting the document to be set to an unsaved state.
What I am not getting is how one would know these specific steps and methods are required. Intuitively it seems one would just add items to the array, and that would be all that's required to have the new values simply show up in the table view for which the array is the data source, so how would one know that the tableView would need to be refreshed with the "reloadData" call? I can see someone (myself) figuring it out by trial and error, but is there some quick resource or guide (ie, some quick flow-chart) either in XCode or elsewhere that indicates for a table view that this would have to be a required action to display the new entry?
If I look at Apple's NSTableView class reference, it claims in the overview that you "modify the values in the data source and allow the changes to be reflected in the table view" which suggest the view is updated automatically, so the requirement to call "reloadData" on the view seems a little obscure.
Look for the guides. In the online class reference for NSTableView, there's a section at the top called "Companion Guides". For NSTableView, it lists the Table View Programming Guide for Mac. (In the prerelease 10.10 docs, the guides are listed under Related Documentation in the left-hand sidebar.)
I could have sworn this same information was available in Xcode's Documentation window, albeit somewhat hidden behind a "More related items" pseudo-link, but when I check right now there's no link to the guide anywhere in the NSTableView class reference. Which is a terrible oversight.
You can also browse or search the Guides section of the developer library.
Familiarity, studying the documentation and possibly reading some good books is the answer. For example, in the docs you quoted (emphasis mine)
you should modify the values in the data source and allow the changes to be reflected in the table view
You should do both these things. If you want it to happen "automatically", look into bindings, which uses several other Cocoa features you won't understand at this point either to do the table data source stuff for you. I'd recommend understanding what is happening manually before handing over control to bindings, so you have some chance of understanding when things go wrong.
As well as looking at the table view documentation, you also need to study the cell, delegate and datasource references. All of those objects work together to give you the functioning table view.

Showing past entries on UITextField

I am writing an app in which I have two UITextFields...
Starting Text
Destination Text
Now once I place values and hit the search or whatever function I want to call, I Want to reuse these values. The app should record and save these values as cache. And should show them when typing or upon a button click. Is that possible to show them just like Dictionary words show up or Which is more preferable tableView or PickerView? If there is any other please let me know.
Definitely use a UITableView. A UIPicherView is used modally most of the time and not for optional suggestions.
Table view is used in Safari, and a lots of other apps:
As for how to cache the data, you have lots of options. It also depends on how much data you expect.
One easy way would be to simply use NSArray. You can very easily write an NSArray to disk in a plist file and read it back when you need it.
Or you could use Core Data, if you expect lots of data and still want high performance. It will be a lot more difficult though to get used to that API if you've never tried it before. Basically you'll need a simple model with one entity called something like SearchEntry that has a single property text. Then you keep adding new instances to your managed object context and can easily filter the existing values.

Simplest possible way to show two items NSTableView from code?

How I can create a code in XCode 4.2.1 what will create NSTableView and add just couple of items to it?
All what I wanted to do is:
1) Window where is NSTableView
2) I have an array of strings in NSArray which I like to show in that NSTableView
3) All of this should be done in code. So I don't want to learn how to add this action happen when you press button (I know already how to do actions when user click buttons etc), I just want that application launch -> draws the table where is those items from my array. That's it, nothing more.
And yeah I have understood that I do not add items to NSTableView directly. That is not the point in this question. I just mean that I want to show couple of items in that table but I have no and kind of clue WHERE I should add my data from my array.
I have tried to google for example pages for hours (just too many and have not find help) but I will always be stucked in the part when
a) I must do something in the Interface Builder and the images of the interface builder are from version 2.x or 3.x and I have 4.2.1 and it is totally different (new to XCode...). Surely I have drawn my TableView element to UI but I mean delegations etc. Are those necessary at all? Can those be made from source code?
b) Code just does not work anymore because language (Cocoa or Objective-C, I don't know) has changed and I don't know how and what I have to do to make it work on newest version of XCode.
c) There is too much different ways told: "use binding", "you must create new class what is NSTableViewDataSource" etc. I have no any kind of clue what is preferred way, is another way optional or it is "you should use this because another is going deprecated soon" or something.
So please, can somebody give help in step-by-step what I exactly have to do? Should I create some bindings? If so, how and where? Do I have to create DataSource component myself? Are those ways valid any more? If I have to, how I can create it? Create a new class and implement it as a NSTableViewDataSource and then use it? Is that way valid any more and if it is, can sombody show code what is as simple as possible?
I have also checked Apple Documentation page many many times, checked those example codes but there is just too much totally unrelated stuff that I just don't understand at this point so they are totally useless (I mean, I don't know what is required for this task, what are not etc.
I would be very happy if somebody can help short tutorial step-by-step what to do. I mean "step-by-step" like:
1) Create new project
2) Draw NSTableView in project
3) Create new class with this name
4) Write this code: blah blah blah
5) Create another class with this name
6) Write this code
7) Run and see those items from array in NSTableView using (bindings/datasource/whatever is preferred).
Thanks :)
Your tableview needs a datasource. Your datasource is a custom class, it implements the "NSTableViewDataSource" protocol. This protocol contains a few methods that you can use to tell the tableview what data you got.
this includes the objectValue ( the value of the NSCell that is displaying your data on the specified row, and a method that returns the amount of rows the tableview has ( the amount of items in your array ).
Here a tutorial I found by googling:
CocoaDev.com NSTableView tutorial
You can also implement the NSTableViewDelegate protocol which allows some greater control. Like what rows you can select, or some extra configuring of a custom tableview cell.
I must say that back in the day when I started developing desktop applications ( only had experience with web technologies ) that this design pattern confused me as well. Hope you will get your mind round it soon.

create a new object

I want to create a new object so as to instantiate and use it several times;
For example, if I want to create an object that has a label and a button inside, how do I? I created a new NSObject but inside it has nothing, then how do I make everything from scratch since there was a viewDidLoad for example (obviously, since it has a view)?
thanks!
Your questions lead me to think that you're really just starting out. There's nothing wrong with that, but rather than trying to summarize several megabytes of documentation in a few paragraphs, I'm just going to point you to the iOS Starting Point. I think that you're just trying to create a container that can hold other UI components? If so, use a UIView for that. However, don't jump in and try to get something specific done without first reading through some of the Getting Started documents -- you'll just end up back here, and we'll just point you back to the docs. You might like the Your First iOS Application guide, as that lets you get your feet wet but explains things along the way.