NSIndexPath indexAtPosition
- (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, ....
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];
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]];
}
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]];
}