jeudi 15 mai 2014

iOS - convertir NSString à NSIndexPath - Stack Overflow


Essentially what I want to do is convert an NSString to a NSIndexPath. When I use S3 to get an object I need to use the requestTag property which takes an NSString. I use this tag to determine which object has finished downloading and therefore which activity indicator to stop spinning. Below is partial code. How can I turn the NSString requestTag into an NSIndexPath?


- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[cell viewWithTag:103];

if (![self.activityIndictatorOn containsObject:indexPath]) {
[self.activityIndictatorOn addObject:indexPath];
[activityView startAnimating];

...
getObjectRequest.requestTag = [NSString stringWithFormat:@"%@", indexPath];
}



-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{

NSLog(@"DOWNLOAD COMPLETE");


[self stopActivityIndicator:request.requestTag];
[self.downloadFinished addObject:request.requestTag];
}



-(void)stopActivityIndicator:(NSString *)rowAtIndexPath
{
//Need to convert rowAtIndexPath to NSIndexPath
//Following line results in warning
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:rowAtIndexPath];
UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[cell viewWithTag:103];
[activityView stopAnimating];
}



[NSString stringWithFormat:@"%@", indexPath]

uses the description method to convert the index path to a string of the form


{length = 2, path = <section> - <row>}

which is a bit difficult to parse back to an index path.


I would therefore suggest to convert the index path to the request string of the format section:row with


NSString *requestTag = [NSString stringWithFormat:@"%ld:%ld", (long)indexPath.section, (long)indexPath.row];

Then you can easily convert it back with


NSArray *tmp = [requestTag componentsSeparatedByString:@":"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[tmp[1] integerValue] inSection:[tmp[0] integerValue]];



Well, you can convert indexPath to NSString and set to requestTag as following:


     getObjectRequest.requestTag = [NSString stringWithFormat:@"%ld,%ld", indexPath.section, indexPath.row];

Then convert it back to NSIndexPath as given below:


    NSArray *rowIndexArray = [rowAtIndexPath componentsSeparatedByString:@","];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:rowIndexArray[1] inSection:rowIndexArray[0]];


Essentially what I want to do is convert an NSString to a NSIndexPath. When I use S3 to get an object I need to use the requestTag property which takes an NSString. I use this tag to determine which object has finished downloading and therefore which activity indicator to stop spinning. Below is partial code. How can I turn the NSString requestTag into an NSIndexPath?


- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[cell viewWithTag:103];

if (![self.activityIndictatorOn containsObject:indexPath]) {
[self.activityIndictatorOn addObject:indexPath];
[activityView startAnimating];

...
getObjectRequest.requestTag = [NSString stringWithFormat:@"%@", indexPath];
}



-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{

NSLog(@"DOWNLOAD COMPLETE");


[self stopActivityIndicator:request.requestTag];
[self.downloadFinished addObject:request.requestTag];
}



-(void)stopActivityIndicator:(NSString *)rowAtIndexPath
{
//Need to convert rowAtIndexPath to NSIndexPath
//Following line results in warning
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:rowAtIndexPath];
UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[cell viewWithTag:103];
[activityView stopAnimating];
}


[NSString stringWithFormat:@"%@", indexPath]

uses the description method to convert the index path to a string of the form


{length = 2, path = <section> - <row>}

which is a bit difficult to parse back to an index path.


I would therefore suggest to convert the index path to the request string of the format section:row with


NSString *requestTag = [NSString stringWithFormat:@"%ld:%ld", (long)indexPath.section, (long)indexPath.row];

Then you can easily convert it back with


NSArray *tmp = [requestTag componentsSeparatedByString:@":"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[tmp[1] integerValue] inSection:[tmp[0] integerValue]];


Well, you can convert indexPath to NSString and set to requestTag as following:


     getObjectRequest.requestTag = [NSString stringWithFormat:@"%ld,%ld", indexPath.section, indexPath.row];

Then convert it back to NSIndexPath as given below:


    NSArray *rowIndexArray = [rowAtIndexPath componentsSeparatedByString:@","];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:rowIndexArray[1] inSection:rowIndexArray[0]];

0 commentaires:

Enregistrer un commentaire