DaSilva
DaSilva

Reputation: 1358

Don't dismiss keyboard with reload collectionViewCell

I have a UICollectionView with custom cells that have a UITextView inside. The user can tap any text inside the text field. When the text field's height gets bigger than the size of the cell, the cell advises the collection view to reload itself.

-(void)updatedCell:(UICollectionViewCell*)cellToUp{

    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cellToUpdateSize];

    BOOL animationsEnabled = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
    [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    [UIView setAnimationsEnabled:animationsEnabled];

}

The problem is that when the cell is "reloading", the keyboard is dismissed and shown up again without any text field associated with it. Is there any solution to avoid dismissing the keyboard?

Thanks in advance.

Upvotes: 5

Views: 1915

Answers (3)

zgjie
zgjie

Reputation: 2276

Do this:

collectionView.collectionViewLayout.invalidateLayout()

Upvotes: 0

frank
frank

Reputation: 2394

Hi,you can use UICollectionView method performBatchUpdates(_:completion:) to modify your related cell's height without to reload the cell. Code like this

    [self.collectionView performBatchUpdates:^{
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:currentRow inSection:currentSection]];
    if (cell) {
        cell.height = [[self.colHeightArray objectAtIndex:currentRow] floatValue];
    }
} completion:^(BOOL finished) {
}];

Hope this help.

Upvotes: -1

Alexander
Alexander

Reputation: 583

You could just override the reload function on your UICollectionView.

Something like:

[super reloadData];
[textView becomeFirstResponder];

Upvotes: 1

Related Questions