Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSIndexPath to NSString-iOS

I want to convert NSIndexPath to NSString. How would I do it? I have to use this:

- (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)sourcePath 
{
    [client deletePath:@"/objc/boston_copy.jpg"];
}

inside commitEditingStyle method where I only get NSIndexPath as input.

- (void)tableView:(UITableView *)aTableView 
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
        forRowAtIndexPath :(NSIndexPath *)indexPath 
{                  
    [self.itemArray  removeObjectAtIndex:indexPath.row];          
    [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                      withRowAnimation:YES];    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
                  withRowAnimation:UITableViewRowAnimationFade];   
}  
like image 214
Namratha Avatar asked May 12 '11 04:05

Namratha


3 Answers

I made this a category on NSIndexPath at one point in time:

@interface NSIndexPath (StringForCollection)

-(NSString *)stringForCollection;

@end

@implementation NSIndexPath (StringForCollection)

-(NSString *)stringForCollection
{
    return [NSString stringWithFormat:@"%d-%d",self.section,self.row];
}

@end
like image 156
Eiko Avatar answered Nov 15 '22 08:11

Eiko


I made this extension to help with debugging:

@interface NSIndexPath (DBExtensions)
- (NSString *)indexPathString;
@end

@implementation NSIndexPath (DBExtensions)
- (NSString *)indexPathString;
{
  NSMutableString *indexString = [NSMutableString stringWithFormat:@"%lu",[self indexAtPosition:0]];
  for (int i = 1; i < [self length]; i++){
    [indexString appendString:[NSString stringWithFormat:@".%lu", [self indexAtPosition:i]]];
  }
  return indexString;
}
@end
like image 32
Dante Bortone Avatar answered Nov 15 '22 06:11

Dante Bortone


You can't convert an NSIndexPath to a string -- an NSIndexPath is effectively just an array of integers. Assuming by "convert" you mean that you want to access the data associated with a particular path, you have to go back to the source of that data.

If you generated the table from an array of objects, which is commonly the case, then you'll simply look at the object at the array index equal to the first element of the indexPath. If the table is sectioned then you need to look at how the data was accessed in order to create the sections -- it likely relates to sorting of the objects based on some object property.

There's no conversion, there's just looking at the data source the same way it was accessed when generating the table.

like image 41
Matthew Frederick Avatar answered Nov 15 '22 07:11

Matthew Frederick