NScollectionView - objective-c

I am new to Cocoa and Objective C. I was able to display collectionviewitems (each containing an image and a text) in NSCollectionView. But now I want to display collectionviewitems (each containing n no. of images and a text) where n is a variable that is known at runtime. Can someone help me how to go about it?

I am not sure I understand what you are trying to accomplish... you can customise the NSCollectionViewItem or even generate it at runtime by subclassing NSCollectionView and using (NSCollectionViewItem *)newItemForRepresentedObject:(id)object. Remember that if you do it binding the view and the object is your business. Another possibility, if you have a finite number of different views you want to show is here: https://stackoverflow.com/a/14699040/2028375

Related

Popup window similar to Xcode's code completion?

I would like to build something similar to the code-completion feature in Xcode 4. (The visual style and behavior, not the data structure type work required for code completion).
As the user is typing, a pop-up window presents other word choices that can be selected.
The Feature in action:
I'm not exactly sure where to start. I am mainly concerned with the visual appearance of the window and how I should populate the list with a given set of words. Later I will get into making the window follow the cursor around the screen and etc.
I am mainly looking for an overview of how to display such data in a "window", and how to cusomize the appearance of the thing so it looks like a nice little informational popup rather than a full-on OS X window.
Just add a subview to your current view that happens to be a tableview. Programmatically cause it to be visible on an event (such as mouseDown), and adjust it's position based on where you want it. You will need to instantiate the proper delegate/datasource methods, but it should be pretty straight forward. You will also need a source for the words you want to use in your autocomplete, and put them in an array or something for your tableview datasource to go through.
Like I said, it's not terribly hard, provided you are comfortable using tableviews and adding views to your existing view. If this doesn't explain enough, leave a comment and I can flesh this comment out more.
Add a (completions) subview to your view and set its visible property to NO. Create a separate AutoComplete object that includes the subview as a property and fills it with potential completions. Your controller can respond to key pressed (key typed) events and give the last word (substring the text from the end to the 1st preceding blank) to the AutoComplete on each event. The basic logic in AutoComplete could be something like:
Given an AutoComplete with a list of known words "dog, spaghetti, minute, horse, spare, speed"
When asked to complete a fragment "sp"
Then the following words should be offered as potential completions, "spaghetti, spare, speed"
Which would imply that you need to instantiate it with a list of words (this could be done in the init of your controller), and create a method "-(NSArray*) completeFragment:(NSString*)fragment;". You could drop this into an OCUnit test case and iterate over your implementation of autocomplete until you get it right. You would then write a method that takes the completions from AutoComplete and lists them in the subview. Even better you might create currentWord and potentialCompletions properties in AutoComplete that gets updated as you send it "newFragment:(NSString*)fragment;" messages. Throw that into OCUnit and work it out then use that potentialCompletions property to drive updated of a the subview (which is probably best modeled as a custom tableView).

Adding data to a scrollview/table

I have an NSCrollView that contains an NSTableView. It has 3 columns and 4 rows. I have 4 NSStrings with content that I need to copy into the scrollview.
Using Xcode 4 I tried connecting the table or the NSTextFieldCell and then adding the text via
[_Cell1 setStringValue:MyString];
But nothing happens. It doesn't get updated.
Any way to do this?
Thank you.
EDIT:
I found the following answer to a similar question. I still am confused but after reading Apple's example about bindings I can only say that this does not make any sense, so much code to achieve something so simple. That's the problem with everything being an object and with OOP in general.
Any simple samples out there? I don't even know how to start setting this or connecting the gazillion things you need to connect to start working with this
You should use the NSTableViewDelegate. That's a set of methods the NSTableView calls to get the data that it should display. You just have to declare the delegate object of the tableview.
Delegate Protocol
NSTableView Tutorial
Unfortunately, you can't "add" or "set" the content of a table view. Like most view objects, a table view doesn't store content; it depends on a controller to provide content when it needs it.
There are two options:
Data source: simplest, easiest to understand
Binding to an array controller: harder to understand, but less work to implement
The best Apple resource on the subject: Populating Cell-Based Table Views from the Table View Programming Guide. If you're struggling, I suggest you start with the data source option. It'll be just a few lines of code, and you can adapt the simple samples from that document.
To populate the table, you need to implement these two methods:
– numberOfRowsInTableView:
– tableView:objectValueForTableColumn:row:
And to change the data, you'll need to implement one more:
- tableView:setObjectValue:forTableColumn:row:
You'll need to set your controller as the data source for the table view in interface builder. And the correct protocol for this is NSTableViewDataSource, not NSTableViewDelegate.
You could use an NSArrayController and bind the table columns' value bindings to the array controller's arranged objects. Then add the values you want to display to the array controller.

Objective-C: How to dynamically create GUI objects?

I`m really new in Objective-C and Mac OSX programming.
Can someone give me simple example for the next task:
I have array of strings (for example ('one','two','three','four')) in my app GUI each string should be represented by row, each row should has Label(with text of string) and CheckBox. Number of strings in array may be different. How i can dynamically create this GUI objects?
It is like in Transmission app, when you open torrent file and window opens with list of files and checkboxes
D you want to create a table? So what you are looking for is a UITableView object whose rows will be dynamically created after you set the 'data source'(in this case the data source can be your viewController that encapsulates that string array) from which it will get the data dynamically.
You'd better have a look at some documentation about UITableView and their programming
If this is not what you are looking for, you can accomplish your goal by creating many UIView dynamically, adding content to them (UILabels, etc) and position them one after the other on the main UIView as subview calculating their frame position and dimension

iPhone SDK basics, one view available from several classes

I'm curious if it's possible to create an application according to window-based pattern, add UILabel, create a new class, and to be able to change UILabel's value from this new class.
After creating all that files we will have:
NewApplicationDelegate.h, .m;
Newclass.h, .m;
MainWindow.xib.
The IBOutlet in this case must be added to MainWindow.xib, and I want to change its value from NewCalss.m. Is it possible? How can I do that?
The point is that I was working only with navigation-based or simple window-based applications before, and now I need to have one view available and changeable (UILabels, for example) from at least 2 other classes.
Thanks.
You will have a UIController object too - right?
That has a handle on the UI elements, UILabel etc, whichever you map through.
You could pass this to the Newclass or you could provide an interface/protocol that the controller exposes that allows Newclass to interact with what you want to do.

How do I populate an NSPopupButton with an array of strings?

Simple question, but I am struggling a little to understand how Interface Builder interacts with the rest of the program developed in X-Code.
My UI has an NSPopupButton that I would like to fill with an array of strings that I provide. I have a controller class that defines the method to execute when the button is clicked. Is that where I would populate the NSPopupButton? It seems like I would need to initialize is somewhere, but I am confused about exactly where to do it. I know I need to use addItemsWithTitles, but where do I call that?
Assuming the list isn't changing over time, awakeFromNib (in the controller) would be a good place to populate the menu. If it's dynamic, then you'd want to populate it when the source of those strings changes.