0

我有一个存储图像和一些字符串的字典(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)?我将这个数组传递给另一个类。数组的出现顺序还没有改变。

感谢您的意见。

4

1 回答 1

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

就放这个

于 2013-03-26T11:41:58.843 回答