UITextView dans un redimensionnement automatique lisse UITableViewCell montre et cache le clavier sur iPad, mais fonctionne sur iPhone

J'ai mis en œuvre un custom UITableViewCell qui comprend un UITextView que l'auto-redimensionne comme les types d'utilisateurs, semblable au champ "Notes" dans l'application Contacts. Il fonctionne correctement sur mon iPhone, mais quand je suis en train de tester dans l'iPad, j'en suis certains un comportement très étrange: Quand vous arrivez à la fin d'une ligne, le clavier se cache pour un millième de seconde, puis se montre à nouveau immédiatement. Je voudrais écrire que c'était juste un bug bizarre, mais en fait elle provoque une perte de données, car si l'on vous frappe, il perd un personnage ou deux. Voici mon code:

Le Code

//returns the proper height/size for the UITextView based on the string it contains.
//If no string, it assumes a space so that it will always have one line.
- (CGSize)textViewSize:(UITextView*)textView {
float fudgeFactor = 16.0;
CGSize tallerSize = CGSizeMake(textView.frame.size.width-fudgeFactor, kMaxFieldHeight);
NSString *testString = @" ";
if ([textView.text length] > 0) {
testString = textView.text;
}
CGSize stringSize = [testString sizeWithFont:textView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
return stringSize;
}
//based on the proper text view size, sets the UITextView's frame
- (void) setTextViewSize:(UITextView*)textView {
CGSize stringSize = [self textViewSize:textView];
if (stringSize.height != textView.frame.size.height) {
[textView setFrame:CGRectMake(textView.frame.origin.x,
textView.frame.origin.y,
textView.frame.size.width,
stringSize.height+10)];  //+10 to allow for the space above the text itself 
}
}
//as per: https://stackoverflow.com/questions/3749746/uitextview-in-a-uitableviewcell-smooth-auto-resize
- (void)textViewDidChange:(UITextView *)textView {
[self setTextViewSize:textView]; //set proper text view size
UIView *contentView = textView.superview;
//(1) the padding above and below the UITextView should each be 6px, so UITextView's
//height + 12 should equal the height of the UITableViewCell
//(2) if they are not equal, then update the height of the UITableViewCell
if ((textView.frame.size.height + 12.0f) != contentView.frame.size.height) {
[myTableView beginUpdates];
[myTableView endUpdates];
[contentView setFrame:CGRectMake(0,
0,
contentView.frame.size.width,
(textView.frame.size.height+12.0f))];
}
}
- (CGFloat)tableView:(UITableView  *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath {
int height;
UITextView *textView = myTextView;
[self setTextViewSize:textView];
height = textView.frame.size.height + 12;
if (height < 44) { //minimum height of 44
height = 44;
[textView setFrame:CGRectMake(textView.frame.origin.x,
textView.frame.origin.y,
textView.frame.size.width,
44-12)];
}
return (CGFloat)height;
}

Les Problèmes

Donc, voici ce qui se passe

  1. Ce code fonctionne 100% correctement sur mon iPhone et dans le simulateur d'iPhone. Comme je l'ai taper le texte, le UITextView pousse en douceur, et la UITableViewCell avec elle.
  2. Sur l'iPad simulateur, cependant, il devient tordu. Il fonctionne très bien pendant que vous tapez sur la première ligne, mais quand vous arrivez à la fin d'une ligne, le clavier disparaît puis réapparaît immédiatement, de sorte que si l'utilisateur continue à taper l'application manque un personnage ou deux.
  3. Voici quelques remarques supplémentaires sur les comportements bizarres que j'ai remarqué qui peut aider à expliquer:
    • Aussi, j'ai trouvé que le retrait des lignes de [myTableView beginUpdates]; [myTableView endUpdates]; dans la fonction textViewDidChange:(UITextView *)textView rend le UITextView développer correctement et de ne pas afficher ou masquer le clavier, mais malheureusement, alors la UITableViewCell ne pousse pas à la bonne hauteur.
    • Mise à JOUR: Suivantes ces instructionsje suis maintenant en mesure d'arrêter l'étrange mouvement du texte; mais le clavier est encore le masquage et l'affichage, ce qui est très étrange.

Quelqu'un a une idée de comment faire pour obtenir le clavier continuellement montrer, plutôt que de les masquer et afficher lorsque vous arrivez à la fin de la ligne sur l'iPad?

P. S.: je ne suis pas intéressée à l'utilisation des ThreeTwenty.

source d'informationauteur Jason