ios - 在自动调整 TextView 大小时输入文本时 UITableView 闪烁/断断续续

标签 ios uitableview

目前我有一个 UITableView,里面有一个可以调整大小的 UITextView。该单元格正在使用 beginUpdates/endUpdates 自动调整大小,但是当它这样做时,表格 View 会断断续续(参见下面的 gif)。

最终结果是一个 UITableViewCell,其中有一个 TextView ,它会根据其内容调整大小。以下是自定义 UITableViewCell 类中的代码,它会导致 UITableView 自行更新。

- (void)textViewDidChange:(UITextView *)textView {
    // This is a category on UITableViewCell to get the [self superView] as the UITableView
    UITableView *tableView = [self tableView];
    if (tableView){
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}

以下是我已经尝试过的事情:

获取当前的 contentOffset 并在 endUpdates 之后重置它但是没有用 在更新前禁用 UITableView 的滚动,然后在更新后启用 我尝试总是从 - (BOOL)textViewShouldEndEditing:(UITextView *)textView 返回 NO 我的 UITableView 单元格高度使用的是 UITableViewAutomaticDimension。欢迎任何其他想法或想法。

这是它的外观示例:

Table View Stutter

我不打算使用任何库,所以请不要对此提出任何建议。

谢谢

编辑:解决方案

在这里找到:I do not want animation in the begin updates, end updates block for uitableview?

归功于@JeffBowen 的伟大发现(尽管它是可行的并且允许我仍然实现支持 iOS 7 的 UITableViewDelegate 方法)。在执行更新之前关闭动画,然后在更新之后启用动画以防止 UITableView 卡顿。

[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

如果您不需要使用 Delegate 方法并且只想为 iOS 8+ 提供一个不那么复杂的解决方案,那么请使用下面@Massmaker 的回答。

最佳答案

只需在调用 beginUpdates 之前禁用动画并在调用 endUpdates 之后重新启用它。

[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

绝对是 hack,但目前有效。感谢我的 friend Beau,他向我指出了 this .

关于ios - 在自动调整 TextView 大小时输入文本时 UITableView 闪烁/断断续续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27460698/

相关文章:

ios - 为特定设备发布 iOS 应用程序

ios - 错误 'GoogleDataTransport/GDTClock.h' 找不到文件

ios - 使自定义动态单元格在 UITableView 中不可重用

ios - 无法在 Collection View 中创建单元格?

IOS7 tableview底部的viewcontrol中多余的空间

ios - 动态 Tableview 单元格

ios - 如何在 IOS7 UITableView 中滚动到顶部?

ios - 设置应用程序的默认iOS本地通知样式

ios - iOS 上 Safari 中 <select> 元素上消失的箭头和样式

ios - tableview swift中的行为空