UITableView单元格自定义cellForRowAtIndexPath的问题

时间:2013-12-12 02:44:17

标签: ios objective-c uitableview

我有一个自定义单元格和一个带数据的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"]);

对象没问题,但我没有归因于单元格层。

EDITED

如果我用以下代码替换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”归属?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您可以按以下方式查看问题:

  1. 确保profissionais不是nil,profissionais.count> = indexPath.row + 1

  2. 确保[profissionais objectAtIndex:indexPath.row]是一个字典,它有一个键@“Nome”

答案 2 :(得分:0)

感谢您的帮助......我找到了解决方案。 NSMutableArray改变了逆行。例如:“Chibungo”是一个Key,“Nome”是一个值,但正确的是“Nome”是一个键,“Chibungo”是一个值。

代码权利是:

//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;

谢谢大家!