4

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.

Neeku
  • 3,538
  • 8
  • 31
  • 42
DaSilva
  • 1,347
  • 1
  • 19
  • 42

2 Answers2

2

You could just override the reload function on your UICollectionView.

Something like:

[super reloadData];
[textView becomeFirstResponder];
Alexander
  • 545
  • 9
  • 18
  • can you provide a better explanation? I can t find a reload function in the UICollectionViewCell. And how the cell knows that should be the firstResponder? Thanks – DaSilva Jul 14 '14 at 18:05
  • Sorry, I meant the reload function on the UICollectionView. I've edited my answer. You'll have to track which cell has the keyboard yourself. Then, in the reload function, get that cell's textView and set it as the first responder. – Alexander Jul 14 '14 at 18:12
  • still not find the "reload method".. With your advise, i can do the same in the cellForItemAtIndexPath? – DaSilva Jul 15 '14 at 09:16
-1

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.

frank
  • 1,978
  • 1
  • 15
  • 19