交换NSMutableArray

时间:2014-01-09 09:19:46

标签: iphone objective-c arrays sorting swap

我有一个包含3个对象的数组,例如[0],[1],[2]以及从委托交换我的TableRows后

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath
{
    NSLog(@"fromIndexPath.row %d",fromIndexPath.row);
    NSLog(@"toIndexPath.row %d",toIndexPath.row);


    NSLog(@"%@",masterArray);

    for (id object in masterArray) {
        for(int indexValue = 0; indexValue < [self.finalContactsArr count]; indexValue++){
            if([[self.finalContactsArr objectAtIndex:indexValue] isEqual:object])
                NSLog(@"the indexValue %d", [self.finalContactsArr indexOfObject:object]);
        }
    }


    item = @"";



/////  This is working fine as below. I want to update the above loop for getting index everytime and log whole array as UI is working absolutely perfect from below code

    item = [self.finalContactsArr objectAtIndex:fromIndexPath.row];
    [self.finalContactsArr removeObject:item];
    [self.finalContactsArr insertObject:item atIndex:toIndexPath.row];


}

我正在寻找新的交换订单,例如,如果我将0,1,2交换为1,0,2或2,0,1,那么应该获得相同的索引订单“indexValue”= 1,0, 2.如果我在一个单独的数组中管理它,那么我将得到存储在masterArray中的原始索引,但事情是例如我交换2,0,1到1,2,0“最后插入第一个索引并且其他的被推下来,因为在UI上提到的removeObject和insertObject逻辑工作“并且现在完成后它将从0,1,2开始。它应该像堆栈推送然后在完成之后应该从0,1,2开始不需要保留任何东西。

4 个答案:

答案 0 :(得分:1)

在tableview对象上调用方法[UITableView moveRowAtIndexPath:toIndexPath:],或者如果交换了很多行,只需使用[UITableView reloadData]

答案 1 :(得分:1)

这是满足您要求的一种解决方案。

第1步: 制作如下物业:

@property (strong, nonatomic) NSMutableArray * staticMyArray;

第2步: 将以下内容添加到- (void)viewDidLoad方法

self.staticMyArray = [[NSMutableArray alloc]init];
self.staticMyArray = [myarray copy];

第3步: 最后在- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath方法

中添加以下内容
NSLog(@"New Order of Indexes is:");
for (id objectFrom_myarray in myarray)
{
    for(id objectFrom_staticMyArray in self.staticMyArray)
    {
        if([objectFrom_myarray isEqual: objectFrom_staticMyArray])
        {
            NSLog(@"%d",[self.staticMyArray indexOfObject: objectFrom_staticMyArray]);//break;
        }
    }
}

如果有效,请告诉我。

答案 2 :(得分:0)

根据Cell-identifier修改数组。您在交换之前和之后创建的。

答案 3 :(得分:0)

请在下面找到答案,我也感谢@Deepak Bharti的回答。

    // 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
{
    NSLog(@"fromIndexPath.row %d",fromIndexPath.row);
    NSLog(@"toIndexPath.row %d",toIndexPath.row);


    if ([matchReplaceOriginalArr count] > 0)
        [matchReplaceOriginalArr removeAllObjects];

    self.matchReplaceOriginalArr = [matchReplaceArr mutableCopy];  ///// Managing this array to hold original index value in each case
    NSLog(@"matchReplaceOriginalArr %@",matchReplaceOriginalArr);


    item1 = @"";


    item1 = [self.matchReplaceArr objectAtIndex:fromIndexPath.row];
    [self.matchReplaceArr removeObjectAtIndex:fromIndexPath.row];
    [self.matchReplaceArr insertObject:item1 atIndex:toIndexPath.row];



    for (id objectFrom_myarray in matchReplaceArr) {

        for(id objectFrom_staticMyArray in matchReplaceOriginalArr)
        {
            if([objectFrom_myarray isEqual: objectFrom_staticMyArray])
            {
                NSLog(@"the indexValue %d \t and Object is %@",[matchReplaceOriginalArr indexOfObject: objectFrom_staticMyArray],objectFrom_myarray);
            }
        }

    }

    NSLog(@"matchReplaceArr %@", matchReplaceArr);


}