将NSString转换为NSIndexPath

Lan*_*don 2 objective-c type-conversion amazon-web-services nsindexpath ios

基本上我想要做的是将NSString转换为NSIndexPath.当我使用S3获取对象时,我需要使用带有NSString的requestTag属性.我使用此标签来确定哪个对象已完成下载,从而确定停止旋转的活动指示器.以下是部分代码.如何将NSString requestTag转换为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];
 }
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 7

[NSString stringWithFormat:@"%@", indexPath]
Run Code Online (Sandbox Code Playgroud)

使用该description方法将索引路径转换为表单的字符串

{length = 2, path = <section> - <row>}
Run Code Online (Sandbox Code Playgroud)

这有点难以解析回索引路径.

因此,我建议在指数路径转换为格式的请求字符串section:row

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

然后你可以轻松地将其转换回来

NSArray *tmp = [requestTag componentsSeparatedByString:@":"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[tmp[1] integerValue] inSection:[tmp[0] integerValue]];
Run Code Online (Sandbox Code Playgroud)