字典和表格部分问题ios

时间:2015-07-20 19:38:55

标签: ios uitableview nsdictionary

我试图找出为什么我的表的各部分只加载字典中的最后一个数组数据。防爆。如果我有3个部分并且我的数组数据大小为3,5,7,那么我的3个部分将有7行(最后一个)。以下是我的代码:

加载数据

display-inline

加载表

-(NSMutableDictionary *) Carrega_musica_por_artista: (NSString *) nome_artista {
    NSMutableDictionary *artista = [NSMutableDictionary new];
    dados = [NSMutableArray new];
    [self Abre_conexao];
    query = [db executeQuery:[NSString stringWithFormat:@"Select m.nome_cantor FROM Musicas_info as m INNER JOIN Campo_pesquisa as cp ON m.id_musica = cp.id_musica where cp.artista like '%%%@%%' GROUP BY cp.artista ORDER BY cp.artista ASC", nome_artista]];
    }
    if (query != nil) {
        while ([query next]) {
            query2 = [db executeQuery:[NSString stringWithFormat:@"Select id_musica, nome_musica, nome_cantor, categoria, tom from Musicas_info where nome_cantor = \"%@\" and ativo <> 0 ORDER BY id_musica ASC", [query stringForColumn:@"nome_cantor"]]];
            if (query2 != nil) {
                [dados removeAllObjects];
                while ([query2 next]) {
                    Construtor_musica *musica_temp = [Construtor_musica new];
                    musica_temp.id_musica = [query2 intForColumn:@"id_musica"];
                    musica_temp.nome_musica = [query2 stringForColumn:@"nome_musica"];
                    musica_temp.autor_musica = [query2 stringForColumn:@"nome_cantor"];
                    musica_temp.categoria = [query2 stringForColumn:@"categoria"];
                    musica_temp.tom = [query2 intForColumn:@"tom"];
                    [dados addObject:musica_temp];
                }
            [artista setObject:dados forKey:[query stringForColumn:@"nome_cantor"]];
            }
        }
    }
    [db close];
    return artista;
}

1 个答案:

答案 0 :(得分:1)

您只分配一个dados数组并不断重用它,这意味着您的字典中的每个键都与同一个数组实例相关联。您需要在while循环中分配一个新的dados数组。

-(NSMutableDictionary *) Carrega_musica_por_artista: (NSString *) nome_artista {
    NSMutableDictionary *artista = [NSMutableDictionary new];
    [self Abre_conexao];
    query = [db executeQuery:[NSString stringWithFormat:@"Select m.nome_cantor FROM Musicas_info as m INNER JOIN Campo_pesquisa as cp ON m.id_musica = cp.id_musica where cp.artista like '%%%@%%' GROUP BY cp.artista ORDER BY cp.artista ASC", nome_artista]];
    }
    if (query != nil) {
        while ([query next]) {
            query2 = [db executeQuery:[NSString stringWithFormat:@"Select id_musica, nome_musica, nome_cantor, categoria, tom from Musicas_info where nome_cantor = \"%@\" and ativo <> 0 ORDER BY id_musica ASC", [query stringForColumn:@"nome_cantor"]]];
            if (query2 != nil) {
                dados = [NSMutableArray new];
                while ([query2 next]) {
                    Construtor_musica *musica_temp = [Construtor_musica new];
                    musica_temp.id_musica = [query2 intForColumn:@"id_musica"];
                    musica_temp.nome_musica = [query2 stringForColumn:@"nome_musica"];
                    musica_temp.autor_musica = [query2 stringForColumn:@"nome_cantor"];
                    musica_temp.categoria = [query2 stringForColumn:@"categoria"];
                    musica_temp.tom = [query2 intForColumn:@"tom"];
                    [dados addObject:musica_temp];
                }
                [artista setObject:dados forKey:[query stringForColumn:@"nome_cantor"]];
            }
        }
    }
    [db close];
    return artista;
}