Anderson Bressane
Anderson Bressane

Reputation: 416

UICollectionView sizeForItemAtIndexPath

I'm having a problem with my UICollectionViewCell when setting it's height programatically. When the collection first load it appears ok, but when the collection is scrolled the cell lose it's height.

This is how it looks before the scrolling: https://dl.dropboxusercontent.com/u/11335239/Captura%20de%20Tela%202013-09-05%20%C3%A0s%2016.18.41.png

This is after the scrolling: https://dl.dropboxusercontent.com/u/11335239/Captura%20de%20Tela%202013-09-05%20%C3%A0s%2016.18.46.png

To calculate se cell height i'm using the method:

- (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;
}

This is what it returns:

Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consequat nunc a erat faucibus, sit 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 elit. In dignissim dignissim pharetra. Proin in ligula a erat vehicula mollis. height: 198.000000 2013-09-06 10:34:03.971 DApp[11370:c07]

Creating the cell:

-(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;
}

Am i forgetting something?

I just just folved my problem! Link below

UICollectionView showing wrong cells after scrolling - dequeue issue?

Upvotes: 0

Views: 6279

Answers (1)

Timothy Moose
Timothy Moose

Reputation: 9915

Comparing the two screenshots, the third row is in the same position, which suggests to me that the cell's are all the correct height. So I would conclude that the problem is with the layouts inside the cells. Since you only see this when you scroll, I would say this is probably a classic cell reuse problem.

When you scroll, cells that go off screen are reused for the cells that are coming on screen. You've got to take this into account when you configure them in cellForRowAtIndexPath by removing any side-effects of their previous use. In other words, you've got to prepare cells for reuse before you configure them. If you're using a custom cell class, you can do this in prepareForReuse. Or you can do it in cellForRowAtIndexPath.

If you need more specific help, please provide details about your cell's view hierarchy, your cellForRowAtIndexPath method, and any other code where you modify the cell's content or layout.

original

I don't see how texto could be the correct value for the given indexPath because you're not setting it in sizeForItemAtIndexPath. So you're calculating the size against the wrong value for textto. So you just need to retrieve the correct value. If you need more help, please share more of your view controller code so we can see your data model.

Upvotes: 1

Related Questions