如何在UITableView中将fromIndex的Row交换到toIndex?

时间:2013-04-01 17:33:15

标签: iphone ios objective-c cocoa-touch

我用 ARC 创建了一个简单的项目,其中我有UITableView。我NSMutableArray NSMutableDictionary。{/ p>

(
        {
        comment = "";
        "field_name" = "field_name 1";
        "main_id" = 1;
        "order_index" = 0;
        "picker_value" = "";
    },
        {
        comment = "";
        "field_name" = "field_name 2";
        "main_id" = 2;
        "order_index" = 1;
        "picker_value" = "";
    },
        {
        comment = "";
        "field_name" = "field_name 3";
        "main_id" = 3;
        "order_index" = 2;
        "picker_value" = "";
    },
        {
        comment = "";
        "field_name" = "field_name 4";
        "main_id" = 4;
        "order_index" = 3;
        "picker_value" = "";
    },
)

"field_name"的值显示在UITableView的每个单元格上。它运作正常。但我想在UITableViewCell上添加交换功能。

例如:

1 value - 0 order_index 
2 value - 1 order_index -----
3 value - 2 order_index      |-> I Want to swap value 2 to 4…(But do not want to change order_index)
4 value - 3 order_index -----

It should Be

1 value - 0 order_index 
4 value - 1 order_index 
3 value - 2 order_index      
2 value - 3 order_index 

value of dictionary will swap but do not  want to change order_index

我了解UITableView

的方法
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row > self.MyArray.count-1)
        return NO;
    return YES;
}

// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath
{
    // here i get fromIndexPath.row and toIndexPath.row
    NSMutableDictionary *oldDict = [NSMutableDictionary dictionary];
    oldDict = [(NSMutableDictionary *)[self.listOfReportField objectAtIndex:fromIndexPath.row] copy];

    [self.myArray removeObject:oldDict];
    [self.myArray insertObject:[self.myArray objectAtIndex:toIndexPath.row-1] atIndex:fromIndexPath.row];
    [self.myArray insertObject:oldDict atIndex:toIndexPath.row];

    // Here i am not swap my row , what should be i need to do here ??
 }

b  作为我的要求首先,我想在这里交换value字典而不是order_index。我无法交换价值。

请给我一些建议。

提前致谢。

2 个答案:

答案 0 :(得分:3)

步骤1:保存订单索引值以及实际索引(在您的情况下为第一个),即"order_index" = 1;和第四个。

第2步:交换(第4个值为第4个值)。

步骤3:由于它是数组,将索引值替换为实际索引(第1个),将第4个相同。

编辑 完整的工作代码:

-(void)swapArray:(NSMutableArray *)array MaintaingOrderFrom:(NSInteger)swapFrom swapTo:(NSInteger)swapTo{
    [array exchangeObjectAtIndex:swapFrom withObjectAtIndex:swapTo];
    NSString *tempOrderIndex=array[swapFrom][@"order_index"];
    array[swapFrom][@"order_index"]=array[swapTo][@"order_index"];
    array[swapTo][@"order_index"]=tempOrderIndex;
}

注意:里面的数组和字典应该是Mutable。

编辑2: 要检查,我需要创建您的数据模型

NSMutableDictionary *dict1=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
                     @"field_name":@"field_name 1",
                     @"main_id":@"1",
                     @"order_index":@"0",
                     @"picker_value":@""
                     }];
NSMutableDictionary *dict2=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
                      @"field_name":@"field_name 2",
                      @"main_id":@"2",
                      @"order_index":@"1",
                      @"picker_value":@""
                      }];
NSMutableDictionary *dict3=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
                      @"field_name":@"field_name 3",
                      @"main_id":@"3",
                      @"order_index":@"2",
                      @"picker_value":@""
                      }];
NSMutableDictionary *dict4=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
                      @"field_name":@"field_name 4",
                      @"main_id":@"4",
                      @"order_index":@"3",
                      @"picker_value":@""
                      }];

NSMutableArray *array=[NSMutableArray arrayWithArray:@[dict1,dict2,dict3,dict4]];

[self swapArray:array MaintaingOrderFrom:1 swapTo:3];

NSLog(@"%@",array);

答案 1 :(得分:1)

NSMutableArray有一个方法- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2 为什么不使用fromIndexPath和toIndexPath?

这两个值来调用该方法

如果我正确地阅读了您的问题,您希望字典中的“order_index”K / V对匹配数组的顺序吗?使用上述方法交换值后,您只需调用:

[[self.myArray objectAtIndex:fromIndexPath.row] setObject:[NSNumber numberWithInt:fromIndexPath.row] forKey:@"order_index"];
[[self.myArray objectAtIndex:toIndexPath.row] setObject:[NSNumber numberWithInt:toIndexPath.row] forKey:@"order_index"];