NSMutableArray索引混淆了

时间:2012-06-26 14:22:09

标签: iphone ios xcode nsmutablearray

我目前正在尝试iCarousel Multiple Carousel示例。 在我的数组中,我使用NSMutableDictionary添加图像:

我有两个:( myImages和myImages2用于我在ViewDidLoad中加载的轮播中的两个插槽)

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

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

在我的iCarousel中:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (carousel == carousel1)
    {
        NSDictionary *obj = [items1 objectAtIndex:index];
        view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"items1"]];
        view.tag = index;
    }else
    {
        NSDictionary *obj = [items2 objectAtIndex:index];
        view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"items2"]];
        view.tag = index;
    }

    return view;
}

在另一个视图中,这些数组也被加载,用户有机会选择要与之比较的图像,

当用户选择图像时,与其标签等效的int将传递到我的两个Carousel所在的位置。

以下是我对它们的比较:

NSInteger image = [prefs integerForKey:@"image"];
NSInteger image1 = [prefs integerForKey:@"image2"];

        if (image == [(UIImageView*)[self.carousel1 currentItemView] tag] || image2= [(UIImageView*)[self.carousel2 currentItemView] tag] || ) {

我以这种方式删除索引:

    NSInteger index = carousel1.currentItemIndex;
    [carousel1 removeItemAtIndex:index animated:YES];
    [items1 removeObjectAtIndex:index]; 

我认为我删除了它错了,因为索引没有更新,我想要的是保持其索引不像这样的图像调整就在这里:

Deleting in NSMutableArray

1 个答案:

答案 0 :(得分:0)

如果我理解你现在要做的是什么,即移动两个轮播直到中间的一个匹配,那么为了没有索引只是消失,那么你可以只是从阵列中删除项目,使用零图像(或带有您选择的支持的有效图像)填充您要删除的位置,并标记表明它已匹配。

这当然取决于我对你想要什么的理解是否有效。