Showing posts with label NSIndexPath. Show all posts
Showing posts with label NSIndexPath. Show all posts

Wednesday, June 19, 2013

NSIndexPath initWithIndexes length example in Objective C (iOS).


NSIndexPath initWithIndexes length

Initializes an allocated NSIndexPath object with an index path of a specific length.

- (id)initWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length

Parameters
indexes
Array of indexes to make up the index path.
length
Number of nodes to include in the index path.

Return Value of [NSIndexPath initWithIndexes length]
Initialized NSIndexPath object with indexes up to length.

NSIndexPath initWithIndexes length example.
NSUInteger x[] = {0 , 0};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
int row = [path row];
int section = [path section];
row = path.row;
section = path.section;

Example of [NSIndexPath initWithIndexes length].
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    //Show the navButtons
    [self navButtonAnimation];

    DetailsStandardCell *cell = (DetailsStandardCell *)textField.superview.superview.superview;

    self.parentController.lastCell = cell;

    //Code to scroll the tableview to the previous indexpath so the previous button will work. NOTE previous button only works if its target table cell is visible.
    NSUInteger row = cell.cellPath.row;
    NSUInteger section = cell.cellPath.section;

    NSIndexPath *previousIndexPath = nil;

    if (cell.cellPath.row == 0 && cell.cellPath.section != 0) //take selection back to last row of previous section
    {
    NSUInteger previousIndex[] = {section -1, ([[self.sections objectForKey:[NSNumber numberWithInt:section - 1]]count] -1)};
    previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];  
    }
    else if (cell.cellPath.row != 0 && cell.cellPath.section != 0)
    {
     NSUInteger previousIndex[] = {section, row - 1};
     previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];

    }

    [self.theTableView scrollToRowAtIndexPath: cell.cellPath.section == 0 ? cell.cellPath : previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

NSIndexPath initWithIndexes length example.
NSUInteger indexes[2];
[indexPath getIndexes:indexes];
indexes[1]--;
NSIndexPath * adjustedIndexPath = [[NSIndexPath alloc] initWithIndexes:indexes
                                                                length:2];
NSManagedObject * managedObject = [self.fetchedResultsController
                                    objectAtIndexPath:adjustedIndexPath];

End of NSIndexPath initWithIndexes length example article.

NSIndexPath initWithIndexes example in Objective C (iOS).


NSIndexPath initWithIndexes

Initializes an allocated NSIndexPath object with an index path of a specific length.

- (id)initWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length

Parameters
indexes
Array of indexes to make up the index path.
length
Number of nodes to include in the index path.

Return Value of [NSIndexPath initWithIndexes]
Initialized NSIndexPath object with indexes up to length.

NSIndexPath initWithIndexes example.
NSUInteger x[] = {0 , 0};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
int row = [path row];
int section = [path section];
row = path.row;
section = path.section;

Example of [NSIndexPath initWithIndexes].
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    //Show the navButtons
    [self navButtonAnimation];

    DetailsStandardCell *cell = (DetailsStandardCell *)textField.superview.superview.superview;

    self.parentController.lastCell = cell;

    //Code to scroll the tableview to the previous indexpath so the previous button will work. NOTE previous button only works if its target table cell is visible.
    NSUInteger row = cell.cellPath.row;
    NSUInteger section = cell.cellPath.section;

    NSIndexPath *previousIndexPath = nil;

    if (cell.cellPath.row == 0 && cell.cellPath.section != 0) //take selection back to last row of previous section
    {
    NSUInteger previousIndex[] = {section -1, ([[self.sections objectForKey:[NSNumber numberWithInt:section - 1]]count] -1)};
    previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];  
    }
    else if (cell.cellPath.row != 0 && cell.cellPath.section != 0)
    {
     NSUInteger previousIndex[] = {section, row - 1};
     previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];

    }

    [self.theTableView scrollToRowAtIndexPath: cell.cellPath.section == 0 ? cell.cellPath : previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

NSIndexPath initWithIndexes example.
NSUInteger indexes[2];
[indexPath getIndexes:indexes];
indexes[1]--;
NSIndexPath * adjustedIndexPath = [[NSIndexPath alloc] initWithIndexes:indexes
                                                                length:2];
NSManagedObject * managedObject = [self.fetchedResultsController
                                    objectAtIndexPath:adjustedIndexPath];

End of NSIndexPath initWithIndexes example article.

NSIndexPath initWithIndex example in Objective C (iOS).


NSIndexPath initWithIndex

Initializes an allocated NSIndexPath object with a one-node index path.

- (id)initWithIndex:(NSUInteger)index

Parameters
index
Index of the item in node 0 to point to.

Return Value of [NSIndexPath initWithIndex]
Initialized NSIndexPath object representing a one-node index path with index.

NSIndexPath initWithIndex example.
Use as below and also Don't forget to release myIndexPath object after using.

NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndex:[myIntObj intValue]];

Example of [NSIndexPath initWithIndex].
- (void)reloadTableView
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"aaa"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"name" object:nil];
    NSLog(@"removeobserver");
    loadImageFinished = YES;
    // if your table has several sections you'll need to adopt the section number
    NSIndexSet *indices = [[NSIndexSet alloc] initWithIndex:0];
    [self.tableView reloadSections:indices withRowAnimation:UITableViewRowAnimationFade];
    [indices release];
}

NSIndexPath initWithIndex example.
- (void) allServersFound {
// called when bonjourservices has listings of machines:
bonjourMachines = [bonjourService foundServers] // NSArray of server names;
[machineListings reloadSections:[[[NSIndexSet alloc] initWithIndex:1] autorelease] withRowAnimation:UITableViewRowAnimationNone];
}

End of NSIndexPath initWithIndex example article.

NSIndexPath indexPathByRemovingLastIndex example in Objective C (iOS).


NSIndexPath indexPathByRemovingLastIndex

Provides an index path with the indexes in the receiving index path, excluding the last one.

- (NSIndexPath *)indexPathByRemovingLastIndex

Return Value
New index path with the receiving index path’s indexes, excluding the last one.

Discussion of [NSIndexPath indexPathByRemovingLastIndex]
Returns an empty NSIndexPath instance if the receiving index path’s length is 1 or less.

Special Considerations
On OS X v10.4 this method returns nil when the length of the receiving index path is 1 or less. On iOS and OS X v10.5 and later this method will never return nil.

NSIndexPath indexPathByRemovingLastIndex example.
You can try this - perhaps equally clunky, but at least a bit shorter:

NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];

End of NSIndexPath indexPathByRemovingLastIndex example article.

NSIndexPath indexPathByAddingIndex example in Objective C (iOS).


NSIndexPath indexPathByAddingIndex

Provides an index path containing the indexes in the receiving index path and another index.

- (NSIndexPath *)indexPathByAddingIndex:(NSUInteger)index

Parameters
index
Index to append to the index path’s indexes.

Return Value of [NSIndexPath indexPathByAddingIndex]
New NSIndexPath object containing the receiving index path’s indexes and index.

NSIndexPath indexPathByAddingIndex example.
NSUInteger newIndexes[] = {0,1};
NSUInteger len = sizeof(newIndexes)/sizeof(NSUInteger); //sorry!
NSIndexPath *baseIndexPath = [NSIndexPath indexPathWithIndexes:newIndexes
                                                        length:len];
NSUInteger i;
for (i=0; i<10; i++) {
    NSIndexPath *newIndexPath = [baseIndexPath indexPathByAddingIndex:i];
    NSLog(@"newIndexPath = %@", newIndexPath);
}
Or you may want to increment an existing indexPath (the first line instantiates an indexPath with the newIndexes array from the example above):

NSIndexPath *someIndexPath = [NSIndexPath indexPathWithIndexes:newIndexes
                                                        length:len];
    NSUInteger newIndexes2[[someIndexPath length]];
    [someIndexPath getIndexes:newIndexes2];

    NSUInteger len2 = sizeof(newIndexes2)/sizeof(NSUInteger);
    NSIndexPath *baseIndexPath2 = [NSIndexPath indexPathWithIndexes:newIndexes2
                                                             length:len2];
    NSUInteger ii;
    NSInteger start = [someIndexPath indexAtPosition:[someIndexPath length] -1];
    for (ii=start; ii<10; ii++) {
        NSIndexPath *newIndexPath2 = [baseIndexPath2 indexPathByAddingIndex:ii];
        NSLog(@"newIndexPath2 = %@", newIndexPath2);
    }
Note that the first example will create children of the original path 0.1, while the second one creates siblings. Mix 'n match as you like.

Example of [NSIndexPath indexPathByAddingIndex].
You can try this - perhaps equally clunky, but at least a bit shorter:

NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];

NSIndexPath indexPathByAddingIndex example.
- (id)objectInArray:(id)array atIndexPath:(NSIndexPath *)path {

    // the end of recursion
    if (![array isKindOfClass:[NSArray self]] || !path.length) return array;

    NSUInteger nextIndex = [path indexAtPosition:0];

    // this will (purposely) raise an exception if the nextIndex is out of bounds
    id nextArray = [array objectAtIndex:nextIndex];

    NSUInteger indexes[27]; // maximum number of dimensions per string theory :)
    [path getIndexes:indexes];
    NSIndexPath *nextPath = [NSIndexPath indexPathWithIndexes:indexes+1 length:path.length-1];

    return [self objectInArray:nextArray atIndexPath:nextPath];
}
Call it like this...

NSArray *array = [NSArray arrayWithObjects:@1, [NSArray arrayWithObjects:@"hi", @"there", nil], @3, nil];

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:1];
indexPath = [indexPath indexPathByAddingIndex:1];

NSLog(@"%@", [self objectInArray:array atIndexPath:indexPath]);

End of NSIndexPath indexPathByAddingIndex example article.

NSIndexPath indexAtPosition example in Objective C (iOS).


NSIndexPath indexAtPosition

Provides the index at a particular node in the index path.

- (NSUInteger)indexAtPosition:(NSUInteger)node

Parameters
node
Index value of the desired node. Node numbering starts at zero.

Return Value of [NSIndexPath indexAtPosition]
The index value at node or NSNotFound if the node is outside the range of the index path.

NSIndexPath indexAtPosition example.
You can use NSIndexPath's -indexAtPosition: method to get the last index:

NSIndexPath *path = ...; // Initialize the path.
NSUInteger lastIndex = [path indexAtPosition:[path length] - 1]; // Gets you the '2' in [0, 2]
In your case, you can use the following (as Josh mentioned in his comment, I'm assuming you're working with a custom subclass of UITableView, because it doesn't have that specific method (-indexPathsForSelectedRows:)):

NSArray *indexes = [self.tableView indexPathsForSelectedRows];
for (NSIndexPath *path in indexes) {
    NSUInteger index = [path indexAtPosition:[path length] - 1];
    NSLog(@"%lu", index);
}
That will print out 0, 1, 2, ....

Example of [NSIndexPath indexAtPosition].
NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];

NSIndexPath indexAtPosition example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
   static NSString *MyIdentifier = @"MyIdentifier";
  
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
   if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
       
   }
  
   // Set up the cell
   int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    cell.textLabel.text=[[stories objectAtIndex: storyIndex] objectForKey: @"title"];
  
   return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic
   
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
   
    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
    NSLog(@"%@",stories );
    // clean up the link - get rid of spaces, returns, and tabs...
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"   " withString:@""];
   
    // open in Safari
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
}

End of NSIndexPath indexAtPosition example article.

NSIndexPath getIndexes example in Objective C (iOS).


NSIndexPath getIndexes

Copies the objects contained in the index path into indexes.

- (void)getIndexes:(NSUInteger *)indexes

Parameters of [NSIndexPath getIndexes]
indexes
Pointer to a C array of objects of size at least the length of the index path. On return, the index path’s indexes.

Discussion of [NSIndexPath getIndexes]
It is the developer’s responsibility to allocate the memory for the C array.

NSIndexPath getIndexes example.
You have to allocate the NSUInteger array of size [indexPath length] and pass it as argument. The return value will be written there. You have to release that array yourself or do nothing it was created on stack like this:

NSUInteger array[[indexPath length]];
[indexPath getIndexes: array];

Example of [NSIndexPath getIndexes].
NSIndexPath *someIndexPath = [NSIndexPath indexPathWithIndexes:newIndexes
                                                        length:len];
    NSUInteger newIndexes2[[someIndexPath length]];
    [someIndexPath getIndexes:newIndexes2];

    NSUInteger len2 = sizeof(newIndexes2)/sizeof(NSUInteger);
    NSIndexPath *baseIndexPath2 = [NSIndexPath indexPathWithIndexes:newIndexes2
                                                             length:len2];
    NSUInteger ii;
    NSInteger start = [someIndexPath indexAtPosition:[someIndexPath length] -1];
    for (ii=start; ii<10; ii++) {
        NSIndexPath *newIndexPath2 = [baseIndexPath2 indexPathByAddingIndex:ii];
        NSLog(@"newIndexPath2 = %@", newIndexPath2);
    }

End of NSIndexPath getIndexes example article.

NSIndexPath compare example in Objective C (iOS).


NSIndexPath compare

Indicates the depth-first traversal order of the receiving index path and another index path.

- (NSComparisonResult)compare:(NSIndexPath *)indexPath

Parameters of [NSIndexPath compare]
indexPath
Index path to compare.
This value must not be nil. If the value is nil, the behavior is undefined.

Return Value of [NSIndexPath compare]
The depth-first traversal ordering of the receiving index path and indexPath.

NSOrderedAscending: The receiving index path comes before indexPath.
NSOrderedDescending: The receiving index path comes after indexPath.
NSOrderedSame: The receiving index path and indexPath are the same index path.
NSIndexPath compare example.
Try [indexPath1 compare: indexPath2] == NSOrderedSame.

Maybe you found a bug in NSIndexPath. If you try to create a new NSIndexPath with a path that already exists you should get that one instead. So isEqual: probably just compares the pointers and not the actual indices stored.

Example of [NSIndexPath compare].
NSComparisonResult result = [indexPath compare:[tempMutArray objectAtIndex:n];

if(result == NSOrderedSame) {
    // do stuff
}
In the Cocoa APIs, there are some non-class-types in use. Most important are those from the Foundation data types, which include

End of NSIndexPath compare example article.

NSIndexPath indexPathWithIndexes length example in Objective C (iOS).


NSIndexPath indexPathWithIndexes length

Creates an index path with one or more nodes.

+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length

Parameters
indexes
Array of indexes to make up the index path.
length
Number of nodes to include in the index path.

Return Value of [NSIndexPath indexPathWithIndexes length]
Index path with indexes up to length.

NSIndexPath indexPathWithIndexes length example.
You might use it like this:

NSUInteger indexArr[] = {1,2,3,4};

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];

Example of [NSIndexPath indexPathWithIndexes length].
This is probably the best you can do:

NSUInteger u_array[] = {0, 0, 0};
NSIndexPath *index_path = [NSIndexPath indexPathWithIndexes:u_array length:3];

NSIndexPath indexPathWithIndexes length example.
NSIndexPath *someIndexPath = [NSIndexPath indexPathWithIndexes:newIndexes
                                                        length:len];
    NSUInteger newIndexes2[[someIndexPath length]];
    [someIndexPath getIndexes:newIndexes2];

    NSUInteger len2 = sizeof(newIndexes2)/sizeof(NSUInteger);
    NSIndexPath *baseIndexPath2 = [NSIndexPath indexPathWithIndexes:newIndexes2
                                                             length:len2];
    NSUInteger ii;
    NSInteger start = [someIndexPath indexAtPosition:[someIndexPath length] -1];
    for (ii=start; ii<10; ii++) {
        NSIndexPath *newIndexPath2 = [baseIndexPath2 indexPathByAddingIndex:ii];
        NSLog(@"newIndexPath2 = %@", newIndexPath2);
    }

End of NSIndexPath indexPathWithIndexes length example article.

NSIndexPath indexPathWithIndexes example in Objective C (iOS).


NSIndexPath indexPathWithIndexes

Creates an index path with one or more nodes.

+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length

Parameters
indexes
Array of indexes to make up the index path.
length
Number of nodes to include in the index path.

Return Value of [NSIndexPath indexPathWithIndexes]
Index path with indexes up to length.

NSIndexPath indexPathWithIndexes example.
You might use it like this:

NSUInteger indexArr[] = {1,2,3,4};

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];

Example of [NSIndexPath indexPathWithIndexes].
This is probably the best you can do:

NSUInteger u_array[] = {0, 0, 0};
NSIndexPath *index_path = [NSIndexPath indexPathWithIndexes:u_array length:3];

NSIndexPath indexPathWithIndexes example.
NSIndexPath *someIndexPath = [NSIndexPath indexPathWithIndexes:newIndexes
                                                        length:len];
    NSUInteger newIndexes2[[someIndexPath length]];
    [someIndexPath getIndexes:newIndexes2];

    NSUInteger len2 = sizeof(newIndexes2)/sizeof(NSUInteger);
    NSIndexPath *baseIndexPath2 = [NSIndexPath indexPathWithIndexes:newIndexes2
                                                             length:len2];
    NSUInteger ii;
    NSInteger start = [someIndexPath indexAtPosition:[someIndexPath length] -1];
    for (ii=start; ii<10; ii++) {
        NSIndexPath *newIndexPath2 = [baseIndexPath2 indexPathByAddingIndex:ii];
        NSLog(@"newIndexPath2 = %@", newIndexPath2);
    }

End of NSIndexPath indexPathWithIndexes example article.

NSIndexPath indexPathWithIndex example in Objective C (iOS).


NSIndexPath indexPathWithIndex

Creates an one-node index path.

+ (id)indexPathWithIndex:(NSUInteger)index

Parameters
index
Index of the item in node 0 to point to.

Return Value of [NSIndexPath indexPathWithIndex]
One-node index path with index.

NSIndexPath indexPathWithIndex example.
You might use it like this:

NSUInteger indexArr[] = {1,2,3,4};

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];

Example of [NSIndexPath indexPathWithIndex].
For an int index:

NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.

To use the indexPath in a UITableView, the more appropriate method is

NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];

NSIndexPath indexPathWithIndex example.
If you have no sections, then you can try the indexPathWithIndex: class constructor:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1]
                      atScrollPosition:UITableViewScrollPositionNone
                              animated:NO];

End of NSIndexPath indexPathWithIndex example article.