NSMutableArray的指数混合起来(NSMutableArray index mixed u

2019-10-16 22:07发布

目前,我想要的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等同于它的标签是传递到我的两个旋转木马是。

这是我如何对它们进行比较:

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

我想我删除它的错误了,因为该指数的arent更新,我想是要保持它的指标没有调整类似这样的图片在这里:

删除在NSMutableArray里

Answer 1:

如果我理解你正在尝试尽现,这是直到中间的一个匹配移动两个旋转木马,然后不具有指标简单地去走,那么你可以只而不是从阵列移除项目,填补了位置您要删除与零图片(或您选择的支持的有效图像),标签,说明它已经被匹配。

这一切当然依赖于我的你想要的理解是否有效。



文章来源: NSMutableArray index mixed up