在 NSMutableArray 中删除 [英] Deleting in NSMutableArray

查看:26
本文介绍了在 NSMutableArray 中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个数组,例如我在每列上有 4 个图像,每个图像都响应其默认索引:

I have an array here, example I have 4 images on each column, each responds to its default index:

当删除图像时,例如索引 1.如下图所示:

When an image is deleted for example index 1. as shown in the image below:

索引变为 0,1,2 :

The index becomes 0,1,2 :

我想要的是 0,2,3(这是原始数组索引):

which I want to be is 0,2,3 (Which is the original array index):

谁能帮助我实现这一目标?

Could anyone help me on how to achieve this?

我的阵列代码:

self.myImages = [NSMutableArray array];
for(int i = 0; i <= 10; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"myImages%d.png", i]]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 
    } 
}     

推荐答案

您可以在字典中放置另一个键,该键将对应于删除任何对象之前的索引.显示它而不是索引,你会得到想要的结果.

You can put another key in your dictionary which will correspond to the index before any removal of objects. Display it instead of the index and you will get the desired result.

编辑 2:

self.myImages = [NSMutableArray array];
for(int i = 0; i <= 10; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"myImages%d.png", i]]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        NSMutableDictionary *container = [[NSMutableDictionary alloc] init];
        [container setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"image"];
        [container setObject:[NSNumber numberWithInt:i] forKey:@"index"];
        [images addObject:container];
        [container release]; // if not using ARC 
    } 
}

当你得到相应的对象时,你会这样做:

And when you're getting the corresponding object, you do:

NSDictionary *obj = [images objectAtIndex:someIndex];
UIImage *objImg = [obj objectForKey:@"image"];
int objIndex = [[obj objectForKey:@"index"] intValue];

这篇关于在 NSMutableArray 中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆