Objective-C: How to dynamically create GUI objects? - objective-c

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

Related

Add items to .plist from and display in custom UITableViewCell

I want to write data to multiple NSMUtableArray's from the user pressing IBAction's that then saves the time they pressed it, Location and os on, to a .plist. I then want to retrieve the data that was written in the plist and display it in a custom UITableViewCell that I have created. There are around 8 - 10 different labels in the cell. I have already made the plist file history.plist and there are a few things in there. I need the data to go in there. And to keep writing data there, such that after the data is gathered, it will then be displayed in the custom cell within the UITableView.
Should Item 0 be a dictionary or array?
So far I have the data being saved as NSUserDefaults
well if you want to use plists like that then keep item 0 as a dictionary... However, I strongly suggest that you use Core Data instead of plists to store your data.

How to read array from UITableView and pass to SQLite

I have some problem understanding how to read an array of x elements from an UITableView and pass it to the SQLite assigning a name to it.
Like a TODO List assigned to people's name.
I have created the database, I can save (null) fields except for the name of the list, but now I do not know how can I read all the fields and pass to my SQLite. Any hint?
In PHP, I usually use a foreach, but I am new to Obj-C and I a little bit blocked.
UPDATE:
The "+" button add a new row, instead the "Save" button has to save this list to an sqlite database.
But I am blocked on how to read and save all the fields.
A UITableView is a view and should not store your data. Usually you will have a ViewController that serves as the DataSource for the table view. This controller usually holds some array of sort that stores the data displayed in the table view.
What is the data source of your tableView ? each tableView object ( witch inherits from UITableView) should have a datasource who gives it the data to display in it's view.
You can use an SQLite wrapper so save/Load easily in/from an SQLite data base. FMDB

NScollectionView

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

Managing data for UITableViews

I have two very simple, identical UITableViews in my app that are populated with thumbnail images named "thumb1.jpg", "thumb2.jpg", etc. These thumbnails have associated original images "1.jpg" and text files "1.txt" used for image processing. Everything is stored in the app's Documents folder.
I want to keep the numbered, ordered naming for these files since it makes displaying the thumbnails in the UITableViews very easy with cellForRowAtIndexPath. I'm currently using a NSMutableArray (index: 1 object:"thumb1.jpg", etc) to track all images in the app.
The issue is that users can add/delete images so maintaining the order is important. For handling adding/deleting I'm looking at using insertObjectAtIndex and removeObjectAtIndex on the NSMutableArray, which will maintain order but will require programmatically changing image and text file names when this happens. For example, if there are five images in the array "0.jpg","1.jpg","2.jpg","3.jpg","4.jpg" and the user deletes the second image ("1.jpg") the array will now have "2.jpg" at index 1 so filenames will have to be changed to "1.jpg", "1.txt", and "thumb1.jpg".
How does this approach sound? I'm new to Objective-C so if you have other functions you'd use, etc I'd be interested to hear your opinion.
Renaming the actual image files themselves doesn't sound like a good idea.
I would implement 2 NSMutableArrays, one to hold the title/description of the image and one to hold the filename (or, if you wanted, the actual UIImage instead). Then if you need to delete, for example the item at index 2, deleting the same object from each of the two arrays will then leave them in sync.
If you start wanting more and more things to be stored for each row, I suggest you implement your own class. You can then implement an array of multiple instances of that class, and each class would have properties such as image, text, thumbnail etc. In fact, because you've already named three separate properties (main image, text and thumbnail) I'm tempted to say you should implement a custom class straight away.
Let me know if this makes sense or if you'd like some code to further illustrate it.

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.