UITableView单元格自定义单元格ForRowAtIndexPath的问题



我有一个自定义单元格和一个包含数据的MutableArray,但当我为MutableArray的一个属性设置单元格文本时,应用程序崩溃。你能帮我吗?

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Cria identificador da célula
static NSString *cellID = @"dadosCell";
//Usa célula customizada da classe ProfissionalCell
ProfissionalCell *cell = (ProfissionalCell *) [self.dadosTableView dequeueReusableCellWithIdentifier:cellID];

//Caso não tenha, aloca uma nova célula
if (cell == nil) {
    //Com StyleSubtitle, são mostrados título e descrição
    cell = [[[NSBundle mainBundle] loadNibNamed:@"ProfissionalCell" owner:self options:nil] objectAtIndex:0];
}
//Profissional selecionado
NSInteger linha = indexPath.row;
Dados *parametro = [profissionais objectAtIndex:linha];
NSLog(@"Nome= %@", cell.cellNome.text);
NSLog(@"Parametro= %@",[[profissionais objectAtIndex:indexPath.row] objectForKey:@"Nome"]);
//Preenche campos
cell.cellNome.text = parametro.nome;
cell.cellProfissao.text = parametro.profissao;
cell.cellEndereco.text = parametro.endereco;
cell.cellIndicacoes.text = parametro.indicacao;
return cell;
}

崩溃发生在以下行:

cell.cellNome.text = parametro.nome;

该行返回"Parametro=null":

NSLog(@"Parametro= %@",[[profissionais objectAtIndex:indexPath.row] objectForKey:@"Nome"]);

对象是可以的,但我不属于单元层。

已编辑

如果我用以下代码替换NSLog:

NSLog(@"Nome= %@", [profissionais objectAtIndex:0]);

结果是:

Nome= {
"2013-12-08 19:35:43" = added;
Chibungo = Nome;
"" = Especialidade;
1 = id;
"Sao Paulo " = Cidade;
"11 9999-9999" = Telefone;
"teste@teste.com" = Email;
"Av. Paulista, 453" = Endereco;
"Servicos Gerais" = Profissao;
SP = Estado;}

如何仅将关键字"Nome"赋予属性?

我认为您尚未初始化mutableArray请检查:-

NSMutableArray *profissionais=[[NSMutableArray alloc]init];

您可以检查以下内容来定位问题:

  1. 确保profiessionais不是零,并且profiessionais.count>=indexPath.row+1

  2. 确保[professionaisobjectAtIndex:indexPath.row]是一个字典,并且它有关键字@"Nome"

感谢您的帮助。。。我找到了解决问题的方法。NSMutableArray是反向变化的。。例如:"Chibungo"是键,"Nome"是值,但正确的说法是"Nom"是键而"Chibung"是值。

正确的代码是:

//Profissional da linha
NSInteger linha = indexPath.row;
NSDictionary *dic = (NSDictionary*)[profissionais objectAtIndex:linha];
//Preenche campos
cell.cellNome.text = [dic objectForKey: @"Nome"];
cell.cellProfissao.text = [dic objectForKey: @"Profissao"];
cell.cellEndereco.text = [dic objectForKey: @"Endereco"];
cell.cellIndicacoes.text = [dic objectForKey: @"Indicacoes"];
return cell;

谢谢大家!

最新更新