簡體   English   中英

NSMutableArray insertObject:atIndex:問題

[英]NSMutableArray insertObject:atIndex: problems

我有這個方法的問題。 我有11個這樣名字的圖像:Articulo 1.png,Articulo 2.png等我不知道為什么,但我的圖像陣列只有最后一張圖像。 難道我做錯了什么?

這是我的例子:

NSMutableArray imagenesA = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
UIImage *image = [[UIImage alloc]init];
UIImageView *imageView = [[UIImageView alloc]init];
NSMutableString *name = [[NSMutableString alloc]init];
NSMutableString *name2 = [[NSMutableString alloc]init];

for (int y = 0; y < 11; y++)  {

    name = [NSMutableString stringWithFormat:@"Articulo %d.png",y+1];
    image = [UIImage imageNamed:name];
    imageView = [[UIImageView alloc] initWithImage:image];
    name2 = [NSMutableString stringWithFormat:@"Articulo %d",y+1];
    [dict  setObject:name2  forKey:@"nombre"];
    [dict  setObject:imageView forKey:@"imagen"];

    [imagenesA insertObject:dict atIndex:y];

    // show all dictionarys
    NSLog(@"DICT: %@", dict);
}

// show index 0 element --> must be Articulo 1 but...
NSDictionary *d = [[NSDictionary alloc] 
                    initWithDictionary:[imagenesA objectAtIndex:0]]; 
NSLog(@"*********************%@***********************",d);

控制台響應:

2011-08-20 14:26:47.411 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 1”; 2011-08-20 14:26:47.411 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 2”; 2011-08-20 14:26:47.412 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 3”; 2011-08-20 14:26:47.412 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 4”; 2011-08-20 14:26:47.413 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 5”; 2011-08-20 14:26:47.414 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 6”; 2011-08-20 14:26:47.414 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 7”; 2011-08-20 14:26:47.415 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 8”; 2011-08-20 14:26:47.415 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 9”; 2011-08-20 14:26:47.416 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 10”; 2011-08-20 14:26:47.416 Catalogo-V1 [28155:207] DICT:{imagen =“>”; nombre =“Articulo 11”; 2011-08-20 14:26:47.416 Catalogo-V1 [28155:207] * ** * ** * *** { imagen =“>”; nombre =“Articulo 11”; } ** * ** * ** * ***

是。 @albertamg在上面的評論中是正確的。 你的for循環應該如下所示。

for (int y = 0; y < 11; y++)  {

    // Other codes here

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict  setObject:name2  forKey:@"nombre"];
    [dict  setObject:imageView forKey:@"imagen"];
    [imagenesA insertObject:dict atIndex:y];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM