1

私が持っているARCで簡単なプロジェクトを作成しましたUITableView。そして、私は持っていNSMutableArrayますNSMutableDictionary

お気に入り

(
        {
        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" = "";
    },
)

key の値は"field_name"my の各 Cell に表示されます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 ??
 }

valueb私の要件として、最初に辞書を交換したい のですが、order_index ここではありません。値を交換できません。

提案をお願いします。

前もって感謝します。

4

2 に答える 2

3

ステップ1:注文インデックス値と実際のインデックス(あなたの場合は1番目)、つまり"order_index" = 1;4番目も保存します。

ステップ 2: スワップ (2 番目の値と 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);
于 2013-04-01T17:40:05.240 に答える