我在我的创建两个NSMutableArray
,viewDidLoad
我将它添加到一个NSMutableDictionary
。当我尝试用一个数组对其进行改组时,没关系。但问题是当我独立地洗牌两个数组时它不起作用,不知何故索引混淆了。
这是我的数组(第一个数组)的代码:
self.items1 = [NSMutableArray new];
for(int i = 0; i <= 100; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"[Images%d.png", i]];
if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){
self.container = [[NSMutableDictionary alloc] init];
[container setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"items1"];
[container setObject:[NSNumber numberWithInt:i] forKey:@"index1"];
[items1 addObject:container];
}
}
NSLog(@"Count : %d", [items1 count]);
[items1 enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
NSLog(@"%@ images at index %d", object, index);
}];
(第二个阵列):
self.items2 = [NSMutableArray new];
for(int i = 0; i <= 100; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"secondImages%d.png", i]];
NSLog(@"savedImagePath=%@",savedImagePath);
if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){
self.container = [[NSMutableDictionary alloc] init];
[container setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"items2"];
[container setObject:[NSNumber numberWithInt:i] forKey:@"index2"];
[items2 addObject:container];
}
}
NSLog(@"Count : %d", [items2 count]);
[items2 enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
NSLog(@"%@ images at index %d", object, index);
}];
我对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;
}
然后是我的洗牌代码(我尝试为另一个数组复制,但也不起作用):
srandom(time(NULL));
NSUInteger count = [items1 count];
for (NSUInteger i = 0; i < count; ++i) {
int nElements = count - i;
int n = (random() % nElements) + i;
[items1 exchangeObjectAtIndex:i withObjectAtIndex:n];
}
我将如何使用上面的代码(或者如果您有其他建议)对两个数组进行洗牌?谢谢
我的另一个问题是,当我尝试为 shuffle 方法子类化该类或使用上面的代码时,它们的索引是混合的。例如:
对象:苹果、球、胡萝卜、狗
索引:1 2 3 4
但在我看来,洗牌时:
对象: 胡萝卜、苹果、狗、芭蕾
索引:2 4 1 3
我还有一个方法,当轮播停止时,它会删除视图中的图像。