以编程方式设置高度时,我的 UICollectionViewCell 出现问题。当集合首次加载时,它看起来不错,但是当集合滚动时,单元格会失去它的高度。
要计算 se 单元格高度,我使用以下方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *texto = ([[self.jsonMensagem objectAtIndex:indexPath.row] objectForKey:@"conteudo"] != [NSNull null]) ? [Metodos formatarTexto:[[[self.jsonMensagem objectAtIndex:indexPath.row] objectForKey:@"conteudo"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]] : @"";
float height = [texto sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0] constrainedToSize:CGSizeMake(220.0, 99999.0f) lineBreakMode:NSLineBreakByWordWrapping].height;
NSLog(@"\n\nText: %@\nheight: %f", texto, height);
tamanho = CGSizeMake(310.0, (height > 50) ? height + 20 : 60);
return tamanho;
}
这是它返回的内容:
文本:Lorem ipsum dolor sit amet,consectetur adipiscing elit。在 consequat nunc a erat faucibus 中,请坐 amet interdum ipsum auctor。Praesent sagittis feugiat aliquet。Phasellus dictum laoreet auctor。Nulla aliquet enim ut ligula sodales,sed tempor nisl interdum。Nullam et libero 精英。在 dignissim dignissim pharetra。Proin in ligula a erat vehicula mollis。高度:198.000000 2013-09-06 10:34:03.971 DApp[11370:c07]
创建单元格:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
perfilMensagemEnviada *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellMensagemEnviada" forIndexPath:indexPath];
cell.textViewMensagem.text = ([mensagem objectForKey:@"conteudo"] != [NSNull null]) ? [Metodos formatarTexto:[[mensagem objectForKey:@"conteudo"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]] : @"";
cell.imageView.image = self.imagemUsuario;
return cell;
}
我忘记了什么吗?
我刚刚解决了我的问题!下方链接