Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 根据表中显示的内容重新排序可变数组_Ios_Uitableview_Ios6_Nsmutablearray_Nsmutabledictionary - Fatal编程技术网

Ios 根据表中显示的内容重新排序可变数组

Ios 根据表中显示的内容重新排序可变数组,ios,uitableview,ios6,nsmutablearray,nsmutabledictionary,Ios,Uitableview,Ios6,Nsmutablearray,Nsmutabledictionary,我有一个dictionary(NSMutableDictionary)的数组(NSMutableArray),它存储图像和一些字符串 @interface SelectViewController () { NSMutableArray *pictureArray; NSMutableDictionary *dict1; UIImage *key1; // UIImage NSString *key2; // file name NSString *key

我有一个dictionary(NSMutableDictionary)的数组(NSMutableArray),它存储图像和一些字符串

@interface SelectViewController () {
    NSMutableArray *pictureArray;
    NSMutableDictionary *dict1;
    UIImage *key1; // UIImage
    NSString *key2; // file name
    NSString *key3; // file path
    NSString *key4; // description
    NSString *key5; // order    
}
我想让用户重新排序行

- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    UIImage *image = [[pictureArray objectAtIndex:indexPath.row] objectForKey:key1];
    cell.imageView.image = image;
    NSString *filename = [[pictureArray objectAtIndex:indexPath.row] objectForKey:key2];
    cell.textLabel.text = filename;
    cell.showsReorderControl = YES;
    return cell;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    NSLog(@"From %i to %i",fromIndexPath.row,toIndexPath.row);
}
因此,每个表行上都会出现一个灰色符号,用户可以对行重新排序。我的问题是如何根据表中显示的内容对数组(pictureArray)重新排序?我将此数组传递给另一个类。阵列的出现顺序尚未改变

谢谢你的建议

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)targetIndexPath
{
    NSUInteger sourceIndex = [sourceIndexPath row];
    NSUInteger targetIndex = [targetIndexPath row];
    if (sourceIndex != targetIndex)
    {
        NSString *item = [pictureArray objectAtIndex:sourceIndex];
        [pictureArray removeObject:item];
        [pictureArray insertObject:item atIndex:targetIndex];
    }
}

把这个放好

哦,天哪。。。就这么简单!?谢谢。我会在3分钟内核对并接受答案。很高兴您理解了我的需要。@Manohar+1您能用这个代码做同样的事情吗[pictureArray exchangeObjectAtIndex:sourceIndex withObjectAtIndex:targetIndex];如果可以的话,它会更优雅,看起来你的代码会稍微打破外观的顺序。很可能,其中一个应该被删除的行没有被删除。