Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSString to NSIndexPath

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];
 }
like image 761
Landon Avatar asked Mar 14 '26 02:03

Landon


1 Answers

[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]];
like image 100
Martin R Avatar answered Mar 16 '26 16:03

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!