I am trying to remove all the items that user has added in NSCollectionView by removing all items from NSMutableArray. It's not working.
I just need to Refresh/Reload by Clear/Remove all items of the NSCollectionView
Try this
NSIndexSet* selectedIndexSet = [_lablesCollectionView selectionIndexes];
NSUInteger index = [selectedIndexSet firstIndex] ;
NSLog(#"index is %d", index);
[lablesArray removeObjectsAtArrangedObjectIndexes:selectedIndexSet];
Hope it helps.
//remove/clear all items from NSCollectionView
for (int i = 0; i < [[arrayController arrangedObjects] count]; i++) {
[arrayController removeObjectAtArrangedObjectIndex:i];
i--;
}
Related
I am trying to make a method in which the data source for my NATableView is cleared, but I cannot figure out how to do this anywhere.
Here is the code I am using to send an array called final to my table view.
// I want to clear it here before filling it again.
for (int i = 0; i < [final count]; i++) {
[myArrayController addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:final[i], #"File Name", nil]];
}
Any help would be great!
If you are using an NSMutableArray and myArrayController is your mutable array you want to call
[myArrayController removeAllObjects];
before you enter the for-loop.
I used AppleKeyboards to get users' keyboard setting before iOS6:
NSArray * keyboards = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleKeyboards"];
for (int i = 0; i < [keyboards count]; i++)
{
NSLog(#"%#", [keyboards objectAtIndex:i]);
}
But after iOS6, I can't use AppleKeyboards as key for the user setting.
Is there any solution for this? Thanks!
You can use UITextInputMode Class to get current text input mode by "+ (UITextInputMode *)currentInputMode".
see also: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextInputMode_Class/Reference/Reference.html
Can I write code like the following? It seems to work, but I want to make sure that it is allowed/safe:
// menu is a NSMenu*
for (NSMenuItem *item in [menu itemArray]) {
if (some condition) {
[menu removeItem:item];
}
}
If it doesn't immediately throw an exception (as modifying an array directly when enumerating it does) then it's probably okay. NSMenu may be giving you a copy of its internal array (assuming it's keeping its items in an NSArray).
For guaranteed safety, however, you might like to do this in two steps. Create another array to hold the items for removal, then enumerate that and do the removal:
NSMutableArray * itemsToRemove = [NSMutableArray array];
for( NSMenuItem *item in [menu itemArray] ){
if( some condition ){
[itemsToRemove addObject:item];
}
}
for( NSMenuItem * item in itemsToRemove ){
[menu removeItem:item];
}
Just for the record:
if you care about memory and dont want to create a secondary array use this:
for (int i = menu.itemArray.count - 1; i >= 0; --i)
{
if( some condition for index i ){
[menu removeItemAtIndex:i];
}
}
I have created an array of 16 CGpoints representing 16 positions on a game board. This is how i set up the array CGPoint cgpointarray[16]; I would like to create a for loop to cycle through each item in the array and check if the touch is within x distance of a position (i have the position as a CGPoint. I don't have much experiance with xcode or objective c. I know the python equivalent would be
for (i in cgpointarray){
//Stuff to do
}
How would i accomplish this? Thanks
for (int i = 0; i < 16; i++){
CGPoint p = cgpointarray[i];
//do something
}
Or if you want to use the NSArray Class:
NSMutableArray *points = [NSMutableArray array];
[points addObject:[ NSValue valueWithCGPoint:CGPointMake(1,2)]];
for(NSValue *v in points) {
CGPoint p = v.CGPointValue;
//do something
}
( not tested in XCode )
This should do it:
for (NSUInteger i=0; i < sizeof(cgpointarray)/sizeof(CGPoint); i++) {
CGPoint point = cgpointarray[i];
// Do stuff with point
}
I would normally go for the NSValue approach above but sometimes you are working with an API where you can't change the output. #Andrews approach is cool but I prefer the simplicity of .count:
NSArray* arrayOfStructyThings = [someAPI giveMeAnNSArrayOfStructs];
for (NSUInteger i = 0; i < arrayOfStructyThings.count; ++i) {
SomeOldStruct tr = arrayOfStructyThings[i];
.... do your worst here ...
}
Could someone point out what is wrong with my for loop? I'm trying to loop thru UIImageViews to make all hidden. All the subviews are UIImageViews. Thanks in advance.
int i;
int num = [[self myView]subviews];
for (i=0; i<num; i++)
{
UIImageView *currentView = [self.tabber.subviews objectAtIndex:i];
currentView.hidden = YES;
}
Ian, Martin and Aram pointed out the error in your code.
I want to point out another way of iterating through an array: Fast Enumeration
for (UIView *view in [self.myView subviews])
{
view.hidden = YES;
}
It has several advantages over C-style counting variable based iterating, such as
cleaner code — no counter variable needed.
Enumeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised.
[[self myView] subviews] refers to the list of subviews, not the number of them. What you want is
for(i = 0; i < [[[self myView] subviews] count]; i++)
A UIView's subview property returns an NSArray.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html
[[self myView]subviews]; returns a NSArray of views, not an int.