Bind NSTableColumn to NSArrayController programmatically - objective-c

How can i bind a NSTableViewColumn to an NSArrayController programmatically?
i have a table with 2 columns that are already bound to the array controller. but i am adding new columns at Runtime, and these need to be bound to the arrangedObjects of the arraycontroller. its a cellbased NSTableview. I only found this code online but am not able to make it work..
NSString *akey = #"somekey";
NSString *keypath = [NSString stringWithFormat:#"arrangedObjects.%#",akey];
[newColumn bind:#"value" toObject:arrayController withKeyPath:keypath options:nil];
i am not sure about the "bind:" part and did not understand the apple documentation, that says that i have to expose a binding..
Any help appreciated.
thanks

replace "value" with NSValueBinding
should work

Related

Objective-C / Load cell from xib / Need explanation

Ok, I know how to load a custom cell from a xib, via this code:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustpmCellView" owner:self options:nil];
cell = (CustomCell *)[nib objectAtIndex:0];
But can someone explain what does the first row do?
I feel really stupid typing that every time and not knowing how exactly it works.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustpmCellView" owner:self options:nil];
loadNibNamed returns all views under your xib. So we are holding that in array. Say here all view under CustpmCellView will be fetched and saved in array nib.
cell = (CustomCell *)[nib objectAtIndex:0];
We are getting first view from array, as that is our desired view, and then we are casting and assigning to cell object.
We need to assign new view to each cell in UITableView, so for that purpose every time new cell is needed, and we do that using above code snippet.
EDIT
[NSBundle mainBundle] , is explained at What the meaning of [NSBundle mainBundle] in iPhone?
According to Docs
loadNibNamed:owner:options:
Unarchives the contents of a nib file located in the receiver's bundle.
- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options
Parameters
name
The name of the nib file, which need not include the .nib extension.
owner
The object to assign as the nib’s File's Owner object.
options
A dictionary containing the options to use when opening the nib file. For a list of available keys for this dictionary, see “Nib File Loading Options.”
Return Value
An array containing the top-level objects in the nib file. The array does not contain references to the File’s Owner or any proxy objects; it contains only those objects that were instantiated when the nib file was unarchived. You should retain either the returned array or the objects it contains manually to prevent the nib file objects from being released prematurely.

Set checkbox inside NSTableView to "YES"

I have an NSTableView with a checkbox column and an NSArrayController.
The NSTableView is bound to the NSArrayController to show its content.
The checkbox column is bound to imgArrayController.arrangedObjects.check1. When the program starts the array is empty. I add values using the arrayController in a loop this way:
[imgArrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSImage imageNamed: [NSString stringWithFormat:#"%#" ,fileName]], #"image",
[NSString stringWithFormat:#"%#" ,fileName], #"filename",
#"YES", #"check1",
nil]];
The correct number of rows are added, however I am getting an error when it tries to set checkbox to YES. This is the error I get:
2013-02-03 19:11:46.357 Stockuploader[561:303] Error setting value for key path check1 of object {
} (from bound object <NSTableColumn: 0x100152280> identifier: check1): [<__NSDictionaryI 0x10010c190> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key check1.
Could somebody please help me to understand what I am doing wrong? The rows appear inside the table but they are all empty.
Try using NSMutableDictionary instead of NSDictionary. Also try using [NSNumber numberWithBool: YES] instead of #"YES" (which is a string, not a boolean).
EDIT:
In a cell-based NSTableView you would need to bind the column (not the cell itself) to the arrayController with a Controller Key of arrangedObjects and a Model Key Path of check1.
In a view-based NSTableView you would need to bind the checkbox control to the Table Cell View with a Model Key Path of objectValue.check1.

arraycontroller nsmutablearray add programmatically

I trying to add a data on table view via Array Controller thats bind to a NSMutableArray.
On the IB property it looks like this :
and on the code I tried to add the NSMutableArray dynamically then reload the view, bu nothings happened.
for(int i=0;i<10;i++){
NSMutableDictionary *group = [[NSMutableDictionary alloc]init];
[group setValue:[NSString stringWithFormat:#"%#-%d", #"Group", i] forKey:#"groupname"];
[contentArray addObject:group];
}
[tableContent reloadData];
I have been google it and browse the same question in stackoverflow, not found a useful one.
any idea ?
Thanks
updated
I wrote above code in File's owner class.
I think the problem is that the array needs to send a KVO notification to the array controller (or maybe it's the table view, I'm not sure). The way to do that is:
self.contentArray = contentArray; (or _contentArray if that's what your ivar is called). I'm assuming that contentArray is a property, if not, you should make it one.

Displaying array in uitableview

I have an array created like this:
[currentQuizArrayQuestions insertObject:[quizArrayQuestions objectAtIndex:randomIndex] atIndex:0];
How do I access its data to display in the table?
I'm trying this:
NSString *cellValue = [currentQuizArrayQuestions objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
I know it's wrong, but how do I get to the data array object stored in the currentQuizArrayQuestions array.
You need to implement the cellForRowAtIndexPath method. See this tutorial on UITableViews: http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/
That one uses a UITableViewController, but the idea is the same.
Edit:
To use a different array, do the same thing with the cell for row at index path method, changing the array that the cellValues come from according to what you need. Then call [tableView reload]; which will run through cellForRowAtIndexPath with the contents of the new array.

ViewTableUI cell loading problem

Helo! How can i convert a NSMutableArray object to a NSString ? i try to do something like
cell.textLabel.text = [cars objectAtIndex:indexPath.row];
But it doesn't work, my app crashes. If i do something like
cell.textLabel.text =#"yes";
The app loads very nicely(but instead of the elements from the "cars" NSMutableArray i have a view table filled with "yes") . Any idea what the problem is ?
Is the NSMutableArray alloc'd and init'd properly? If you don't call cars = [[NSMutableArray alloc] init] before you use it, the array will not respond to adding objects to it. Make sure that's not the case. Also make sure that the elements in the cars array are NSStrings.
Make sure your cards contains enough cars, if the indexPath.row is large than the cards's count-1, it will crash.