首页 > 代码库 > 坑爹小问题-table改变frame动画影响cell

坑爹小问题-table改变frame动画影响cell

反正就是很坑爹.简单说就是这样吧,做一个简单的聊天界面,对话框左一个右一个那样子.(界面可以脑补微信)底下的键盘弹起来时候,展示对话的table的frame要相应的缩小,隐藏键盘又要恢复.就这么简单一个代码:

 1 - (void)keyboardFrameChange:(NSNotification *)sender 2 { 3     NSDictionary *userInfo = sender.userInfo; 4     CGRect keyboardFrame; 5     [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardFrame]; 6     NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 7     NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; 8      9     //坑爹的frame调整动画10     [UIView beginAnimations:nil context:nil];11     [UIView setAnimationDelegate:self];12     [UIView setAnimationDuration:[duration doubleValue]];13     if ([duration doubleValue] != 0)14     {15         [UIView setAnimationCurve:[curve intValue]];16     }17     _tfToolBar.frame = [self changeYForFrame:_tfToolBar.frame withY:keyboardFrame.origin.y-_tfToolBar.frame.size.height];18     _mainTabel.frame = [self changeHeightForFrame:_mainTabel.frame withHeight:keyboardFrame.origin.y-_tfToolBar.frame.size.height];19     [UIView commitAnimations];20     21     //让表滚到最下面一行22     if ([_chatArr count])23     {24         NSIndexPath *lastPath=[NSIndexPath indexPathForRow:[_chatArr count]-1 inSection:0];25         [_mainTabel scrollToRowAtIndexPath:lastPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];26     }27 }

 

然后奇葩的事发生了,table拉长的时候,原来最顶上未显示的几个cell要被绘制出来.但是因为table的frame改变实在Animations里面进行的,因此绘制cell也带上了动画效果= =

调试过程就不多说了,反正刚开始没找到症结(开始还以为是单元格重用造成的).知道是怎么回事了就简单了.20行的地方加个[_mainTabel reloadData]就万事大捷了...