简体   繁体   English

如何将NSIndexPath转换为NSString-iOS

[英]How to convert NSIndexPath to NSString-iOS

I want to convert NSIndexPath to NSString . 我想将NSIndexPath转换为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. 在commitEditingStyle方法中,我只获得NSIndexPath作为输入。

- (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];   
}  

I made this a category on NSIndexPath at one point in time: 我在一个时间点在NSIndexPath上创建了这个类别:

@interface NSIndexPath (StringForCollection)

-(NSString *)stringForCollection;

@end

@implementation NSIndexPath (StringForCollection)

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

@end

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

You can't convert an NSIndexPath to a string -- an NSIndexPath is effectively just an array of integers. 您无法将NSIndexPath转换为字符串 - NSIndexPath实际上只是一个整数数组。 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. 如果您从一个对象数组生成表(通常是这种情况),那么您只需查看数组索引处的对象,该索引等于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. 没有转换,只是在生成表时以与访问数据源相同的方式查看数据源。

extension NSIndexPath {
    var prettyString: String {
        var strings = [String]()
        for position in 0..<length {
            strings.append(String(indexAtPosition(position)))
        }
        return strings.joinWithSeparator("_")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM