NSMutableArray 索引混淆 [英] NSMutableArray index mixed up
问题描述
我目前正在尝试 iCarousel
Multiple Carousel
示例.在我的数组中,我使用 NSMutableDictionary 添加了图像:
I'm currently trying the iCarousel
Multiple Carousel
example.
Here in my array, I've add images with NSMutableDictionary:
我有两个这样的:(myImages 和 myImages2 用于我在 ViewDidLoad 中加载的轮播中的两个插槽)
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
}
}
在我的 iCarousel 中:
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,
当用户选择一个图像时,相当于它的标签的 int 被传递到我的两个 Carousel 所在的位置.
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:
推荐答案
如果我明白你现在想要做什么,那就是移动两个轮播直到中间的一个匹配,然后不让索引直接消失,那么您可以不从数组中删除该项目,而是使用 nil 图像(或带有您选择的支持的有效图像)填充您尝试删除的位置,以及一个表明它已经匹配的标签.
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.
这篇关于NSMutableArray 索引混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!