/ / definir a altura da linha de acordo com o tamanho da corda? - iphone, ios, ipad

definindo a altura da linha de acordo com o tamanho da string? - iphone, ios, ipad

Tenho um aplicativo no qual desejo alterarminha altura da célula de acordo com o conteúdo da string de mensagem recebida. Estou fazendo assim no método de delegado hieghtforrow lateral do modo de exibição de tabela.

int rowHeight =0.0f;

UITableViewCell *cell = [ self.mtableview cellForRowAtIndexPath:indexPath.row];

CGSize size = [cell.textLabel.text  sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300, 5000) lineBreakMode:UILineBreakModeWordWrap];// calculate the height

rowHeight = size.height+10; // I use 10.0f pixel extra because depend on font

return rowHeight;

mas não está travando no meu aplicativo. Alguém pode dar uma olhada nisso?

Respostas:

1 para resposta № 1

Eu gostaria de ver o seu cellForRowAtIndexPath método para entender como você está recuperando o texto para o rótulo.

Você está no caminho certo para fazer chamadas sizeWithFont mas você precisa de duas coisas para determinar isso com sucesso:

  1. O tamanho da fonte do rótulo (codificado em 13.0)
  2. E o texto para determinar o tamanho (o qual você está tentando extrair do UILabel)

(Codifiquei o tamanho da fonte no código em 13.0 não é necessariamente uma boa ideia, porque se você quiser alterá-lo para a célula, será necessário lembrar de alterá-lo em heightForRowAtIndexPath e em qualquer outro lugar, mas esse é um problema diferente).

Em vez de extrair o texto do rótulo do UILabel em si, determine o texto de qualquer estrutura de dados que gerou / contém o texto em primeiro lugar. É por isso que ver o seu cellForRowAtIndexPath método seria útil.

Não ligue cellForRowAtIndexPath a partir de heightForRowAtIndexPath também, os métodos não se destinam a ser usados ​​dessa forma.

Aqui está um exemplo simples, que posso refinar quando você postar seu cellForRowAtIndexPath código:

//ASSUME that self.arrayOfStrings is your data structure where you are retrieving the label"s text for each row.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight =0.0f;
NSString *stringToSize = [self.arrayOfStrings objectAtIndexPath:indexPath.row];
CGSize size = [stringToSize  sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300, 5000) lineBreakMode:UILineBreakModeWordWrap];
rowHeight = size.height+10;
return rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.arrayOfStrings objectAtIndexPath:indexPath.row];
return cell;
}

0 para resposta № 2

Remover UITableViewCell *cell = [ self.mtableview cellForRowAtIndexPath:indexPath.row]; do seu código. Você não precisa adicionar células no método HeightForRow.