
在监听键盘弹出的方法keyboardWillShowNotification:这样写了
_commentRow//评论的是哪一行
_entries//数据源
Model //数据模型
NSInteger tableViewOffsetY = 0;
for (int i = 0; i <= self.commentRow; i++) {
Model *model = [self.entries objectAtIndex:i];
tableViewOffsetY += model.totalHeight;
}
CGFloat scrollOffsetY = tableViewOffsetY - self.tableView.bounds.size.height;
if (scrollOffsetY > 0) {
[UIView animateWithDuration:0.25 animations:^{
self.tableView.contentOffset = contentOffset;
}];
}
tableViewOffsetY 的类型是NSInteger,而不是CGFloat类型,造成精度丢失,造成的问题就是随着_commentRow的越来越大,列表滚动的偏移量变小,这种变小是会累计的。
为了解决这个问题可是费了一番功夫,又是查看数据模型totalHeight方法是够准确,又会单独写工程demo测试排除项目其他模块的影响,最后才发现原来是很常见的精度丢失问题
原因分析出来了,希望各位developer注意
网友评论