NSCollectionView doesn’t show its contents - objective-c

What I want to do is create a simple NSCollectionView in my application and populate it with images.
I have managed to design/link everything in Interface Builder and I try to load an array of whatever into the collection view so that it will display the view (nib-file) linked to it in the interface builder, but I had no luck so far. There is no way I can make to collection view display anything.
NSMutableArray * array = [[NSMutableArray alloc] init];
[array addObject:#"Test"];
[array addObject:#"Test"];
[array addObject:#"Test"];
[array addObject:#"Test"];
[array addObject:#"Test"];
[myCollectionView setContent:array];
I have followed 2 guides I found on Google but they seem to be deprecated, they wont work in Xcode 4.
I'm desperate please help me. I just want to make the NSCollectionView to display anything. I'm running Lion and xcode 4.1

I solved the problem.
In order for the NSCollectionView to show anything, the NSArray that you provide to it must have nil as the last element.
After that the NSCollectionView was displaying beautifully.

Related

How to get current index of UIBarButtonItem to remove it by clicking event?

I'm in the process of learning Objective-C and iOS development. So, I implemented removing of UIBarButtonItem from UIToolBar on UIControlEventTouchDown event in the selector. But this works really bad and the code is not very declarative as you see:
- (void)barButtonClicked:(id)sender
{
NSArray * const itemsArray = userToolbar.items;
NSMutableArray * mutableItems = [NSMutableArray arrayWithArray:itemsArray];
[mutableItems removeObjectAtIndex:0];
[userToolbar setItems: mutableItems animated:YES];
}
So as you see I removed item accordingly to its index in the userToolbar items array. It's not what I really want. I have on my UIToolBar nearly 10-12 UIBarItemButtons and I want define one common event for them all: removing it from the bar by clicking on it. So I need something like this: [mutableItems removeObjectAtIndex:sender.currentIndexInToolBarItemsArray] So, the question how can I implement this?
Instead of removing the object at the constant index 0, use the removeObject: method of NSMutableArray:
[mutableItems removeObject:sender];

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.

NSTableView: programmatically adding one row results in selection of many rows with the same values?

now, i've been reading just about everything about NSArrayController.
i have properly setup my xib, the NSArrayController, NSTableView and the bindings -- everything's working except for one odd behavior of my NSTableView.
First some code to picture the scene:
#implementation MyAppDelegate
#synthesize arrayController, dataArray;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.dataArray = [NSMutableArray array];
}
- (IBAction)myControllerAddAction:(id)sender {
NSString *val = [[NSDate date] description];
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithObjectsAndKeys:val, #"date", nil];
[serversController addObject:item];
}
- (IBAction)myArrayAddAction:(id)sender {
NSString *val = [[NSDate date] description];
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithObjectsAndKeys:val, #"date", nil];
[[self mutableArrayValueForKey:#"dataArray"] addObject:item];
}
#end
using -mutableArrayValueForKey:#"myNSArrayControllerStore" and adding objects via the returned NSMutableArray proxy's -addObject:myDictionary the array controller instructs the table view to draw the new content but it'll select all rows with the same values in their NSCells:
using the array controller's -addObject:myDictionary it'll draw the new row and select the newly added one:
(on both screens i've been continuously clicking multiple times per second)
Why is that?
Do i need to handle proper row selection on my own when using [NSMutableArray -addObject:]?
Or is this a bug?
Mac OS 10.7.3
Xcode 4.3
Base SDK 10.7
Deployment target 10.6
Using an array controller's -addObject: method is the right way to handle insertion. It ensures the controller sends the right messages to objects which are bound to it.
It sounds like the array controller approach is working correctly.
It would be useful to be able to bind array controllers to mutable arrays, and for them correctly to monitor insertions to the bound arrays, but I don't think they are designed to work in this manner.

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.

Add NSMutableArray to another NSMutableArray

Hey! I've been trying to add an array to an NSMutableArray using the addObject syntax. The app crashes without any explicit mention of an error. Could you please tell me how this is done?
I have a class Stack that creates an array. So I call that class using an instance called tem that I have created. Hence, [self tem] is my call to the array. Through the program I merely add UILabels to the array(I know you'd suggest adding a string and then changing to UILabels, but I need to do it this way) and towards the end, I'd like to add this array to my 'list' array.
-(Stack *)tem {
if(!tem)
tem=[[Stack alloc]init];
return tem;
}
-(void)viewDidLoad{
//code
list=[NSMutableArray arrayWithArray:[self list].array];
}
-(IBAction)opPr:(UIButton *)sender
{
if([[[sender titleLabel]text] compare: #"="]==0) {
//code
UILabel *t=[[UILabel alloc]init];
//complete creating label
[[self tem]push:t];
//add above label to tem array
[list addObject:[self tem].array];
[table reloadData];
}
}
OK, this answer got all cluttered with edits. I've edited it to be more clear, at the possible expense of understanding the thread of how we arrived at the final answer.
The final answer
The answer was to change:
list=[NSMutableArray arrayWithArray:[self list].array];
To:
list = [[NSMutableArray alloc] init];
(...Which isn't really the best way to create an NSMutableArray, but it may work anyway.)
This was based on an erroneous version of the asker's code
Based on your comment that it crashes after [list addObject:[self tem].array];, I must conclude that your instance variable list is of type Stack*, and that Stack is not a subclass of NSMutableArray. If so, that's your issue.
In that case, changing that line to [[list array] addObject:[self tem].array]; should fix it.
This is just good advice
As an aside, NSMutable array is perfectly capable of acting as a stack without modification. Example:
NSMutableArray* ar = [NSMutableArray arrayWithCapacity:someCapacity];
// Push
[ar addObject:narf]; // narf being some object reference
// Pop
id popped = [ar lastObject];
[ar removeLastObject];
If you want to encapsulate stack behavior in a semantically consistent way, you can add push and pop methods to NSMutableArray using a category. This would make your code simpler and less prone to error.
This was my first stab at answering the question, before any code had been posted
This is a stab in the dark since you've not posted any code. BUT. One way to accomplish that with arrays is by creating arrays using [NSArray arrayWithObjects:obj obj ... nil] and omitting the nil terminator on the list of objects. Are you by any chance doing that?