arraycontroller nsmutablearray add programmatically - objective-c

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.

Related

Adding managedObjectContext objects with an NSArrayController

I have this application that is using core data and an NSArrayController to manage some objects in a table. I have the code below to pick up some objects on a directory. My questions is about the section below labeled "Handle Files". I create a new Video object using the url, I copy the metadata attributes using a custom function I wrote. The object is now inserted in the managedObjectContext. My question is, since I have my NSArrayController bound to my managedObjectContext, why do I have to still do [self addObject:newVideo] to have the object shown on my table? Is there a way to force the array controller to pull the object from the managedObjectContext without having to manually add it? It will be a hassle having to be updating both things every time I add or remove an object.
for (NSURL *url in _dirEnumerator) {
NSNumber *_isDirectory = nil;
[url getResourceValue:&_isDirectory forKey:NSURLIsDirectoryKey error:NULL];
if (![_isDirectory boolValue]) {
if (([_mediaTypes containsObject:[[url pathExtension]uppercaseString]])) {
// Handle the files
Video *newVideo = [NSEntityDescription insertNewObjectForEntityForName:#"Video" inManagedObjectContext:_managedObjectContext];
[newVideo copyAttributesFrom:url];
[self addObject:newVideo];
NSLog(#"Inserting video: %#",[newVideo valueForKey:#"name"]);
}
}
}
Well, I had my bindings all wrong an the array controller was not feeding my table correctly. You cannot sneak objects behind the array controller, if you implement the array controller you must let him do his job and that includes adding and removing objects. He will take care of letting the tableview know when things have changed.

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];

NSCollectionView doesn’t show its contents

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.

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?