本文介绍了如何在UITableView中将row从fromIndex交换到toIndex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ARC 创建了简单项目,其中包含 UITableView 。我有 NSMutableArray of NSMutableDictionary

I created simple project with ARC, in which I have UITableView. And I have NSMutableArray of NSMutableDictionary.

like

(
        {
        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 上插入交换功能。

Value of key "field_name" is displayed on each Cell of my UITableView. It is working properly. But I want to put swap functionality on my 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 这里。我不能交换价值。

b As my Requirement first I want to swap value of dictionary but not order_index here. I am not able to swap value.

请给我建议。

提前感谢。 >

Thanks in advance.

推荐答案

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

Step 1: Save the order index value as well it actual index (1st in your case) i.e. "order_index" = 1; and 4th one also.

步骤2:互换(第二个值与第四个值)。

Step 2: Swap(2nd value with 4th value).

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

Step 3: As it is array, replace the index value to the actual index(1st) and same for 4th.

EDIT: 完整的工作代码:

the full working code:

-(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: 要检查,我需要创建您的数据模型

EDIT 2: To check, I need to create your data model

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);

这篇关于如何在UITableView中将row从fromIndex交换到toIndex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:43