gpt4 book ai didi

ios - 字典和表格部分问题 ios

转载 作者:行者123 更新时间:2023-11-29 01:49:58 25 4
gpt4 key购买 nike

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

加载数据

-(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;
}

加载到表上

- (void)viewDidLoad
{
NSMutableDictionary *dados_artista = [musica_db Carrega_musica_por_artista:self.textfield.text];
NSArray *artistas = [[dados_artista allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *custom_cell = @"celula_musica";
Celula_musica *cell = (Celula_musica *) [self.tabela_musica dequeueReusableCellWithIdentifier:custom_cell];
if (!cell) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Celula_musica_ipad" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Construtor_musica *mus_temp = [[dados_artista objectForKey:[artistas objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell = [montagem_musica Montagem_celula:mus_temp celula:cell];
return cell;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [artistas objectAtIndex:section];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[dados_artista objectForKey:[artistas objectAtIndex:section]] count];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [artistas count];
}

最佳答案

您只分配一个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;
}

关于ios - 字典和表格部分问题 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com