根据表中出现的内容重新排序MutableArray

时间:2013-03-26 11:37:15

标签: ios uitableview ios6 nsmutablearray nsmutabledictionary

我有一个数组( NSMutableArray )的字典( NSMutableDictionary ),用于存储图像和一些字符串。

@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)?我把这个数组传递给另一个类。阵列的外观顺序尚未改变。

感谢您的建议。

1 个答案:

答案 0 :(得分:2)

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

只需放置

即可