NSMutableArray index mixed up

爷,独闯天下 提交于 2019-12-11 11:06:12

问题


I'm currently trying the iCarousel Multiple Carousel example. Here in my array, I've add images with NSMutableDictionary:

I have two of these: ( myImages and myImages2 for my two slot in carousel loaded in 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 
    } 
}

in my 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;
}

In another View, these arrays are also loaded, the user has a chance to pick an image to compare to with,

when an user pick an image an int equivalent to its tag is pass to where my two Carousel is.

Here is how I compare them:

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

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

I delete an index this way:

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

I think I'm deleting it the wrong away, because the index arent updated, what i wanted is to maintain its index not adjust like this images right here:

Deleting in NSMutableArray


回答1:


If I understand what you are trying to do now, which is to move the two carousels until the middle one matches, then to not have the indexes simply go away, then you could just rather than removing the item from the array, fill the location you are trying to delete with a nil image (or a valid image with the backing of your choosing), and a tag stating it has already been matched.

This is all of course dependent on whether my understanding of what you want is valid.



来源:https://stackoverflow.com/questions/11209480/nsmutablearray-index-mixed-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!