Como posso criar uma variável dimensionada UITableViewCell?
-
22-07-2019 - |
Pergunta
Eu não consigo obter o texto para realmente abranger várias linhas. As alturas parecem corretas. O que eu estou ausente?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease];
CGRect frame = cell.contentView.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
[cell.contentView addSubview:myLabel];
[myLabel release];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
Além disso, o que estou ausente que faz com que os rótulos aparecem mais do que a célula da tabela?
Solução
Tweetero
fornece um exemplo disso na MessageListController.m
. O código não torna a tela a seguir:
(Pic é retirado Mashable ).
O esquema básico de implementação:
-
Ao construir um
UITableViewCell
, criar e adicionar umUILabel
como um subview da maneira mostrada natableviewCellWithReuseIdentifier:
. Olhar para a criação de etiquetaTEXT_TAG
. -
quando enriquecendo o
UITableViewCell
com vistas, garantir que você formatar rótulo corretamente, como é feito emconfigureCell:forIndexPath
, semelhante procurar oTEXT_TAG
tag rótulo. -
Voltar a altura apropriada para cada célula, como é feito em
tableView:heightForRowAtIndexPath
.
Outras dicas
myLabel.numberOfLines = 2;
Verifique a docs para informação completa sobre como usar essa propriedade.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return theSizeYouWantYourCellToBe;
}