2013-04-01 65 views
1

我用ARC創建了簡單的項目,其中我有UITableView。我有NSMutableDictionaryNSMutableArray如何在UITableView中將fromIndex換行到toIndex?

(
     { 
     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這裏。我無法交換價值。

請給我建議。

在此先感謝。

回答

3

步驟1:保存訂單索引值以及它的實際索引(在您的情況下爲第一個),即"order_index" = 1;和第四個。

第2步:交換(第4個值的第2個值)。

第3步:因爲它是數組,所以將索引值替換爲實際索引(1st),對於第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; 
} 

注:數組和字典裏面應該是可變的。

編輯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

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