如何将NSIndexPath转换为NSString-iOS

时间:2011-05-12 04:20:21

标签: ios nsstring nsindexpath

我想将NSIndexPath转换为NSString。我该怎么办? 我必须用这个:

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

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

4 个答案:

答案 0 :(得分:5)

我在一个时间点将其作为NSIndexPath的类别:

@interface NSIndexPath (StringForCollection)

-(NSString *)stringForCollection;

@end

@implementation NSIndexPath (StringForCollection)

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

@end

答案 1 :(得分:3)

我做了这个扩展来帮助调试:

@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

答案 2 :(得分:1)

您无法将NSIndexPath转换为字符串 - NSIndexPath实际上只是一个整数数组。假设通过“转换”表示您想要访问与特定路径关联的数据,您必须返回到该数据的来源。

如果从对象数组生成表(通常是这种情况),那么您只需查看数组索引处的对象,该索引等于indexPath的第一个元素。如果表是分段的,那么你需要查看如何访问数据以创建部分 - 它可能与基于某些对象属性的对象排序有关。

没有转换,只是在生成表时以与访问数据源相同的方式查看数据源。

答案 3 :(得分:0)

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