ios – 在自动调整TextView中输入文本时,UITableView闪烁/停顿

目前我有一个UITableView,其中有大小的UITextView.单元格使用beginUpdates / endUpdates自动调整大小,但是当它执行表视图stutters(参见下面的gif).

最终结果是一个UITableViewCell,其中有一个文本视图,它根据内容进行调整大小.这是自定义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.欢迎任何其他想法或想法.

以下是一个示例:

我不想使用任何图书馆,所以请不要建议.

谢谢

编辑:解决方案

发现于:I do not want animation in the begin updates,end updates block for uitableview?

感谢@JeffBowen为一个伟大的发现(虽然hacky它是可行的,并允许我仍然实现UITableViewDelegate方法来支持iOS 7).在执行更新之前关闭动画,然后在更新后启用,以防止UITableView发生口吃.

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

如果您不需要使用Delegate方法,并且只需要一个较少的hacky解决方案,只有iOS 8,然后去@ Massmaker的答案下面.

解决方法

我的解决方案(适用于iOS 8)首先在我的viewController viewDidLoad中设置
self.tableView.rowHeight = UITableViewAutomaticDimension;
// this line is needed to cell`s textview change cause resize the tableview`s cell
self.tableView.estimatedRowHeight = 50.0;

那么结合这篇文章solution in Swift和一些肮脏的想法
我在我的牢房里设置了一个属性,叫做

@property (nonatomic,strong) UITableView *hostTableView;

和cell-s(void)textViewDidChange:(UITextView *)textView

CGFloat currentTextViewHeight = _textContainer.bounds.size.height;
CGFloat toConstant = ceilf([_textContainer sizeThatFits:CGSizeMake(_textContainer.frame.size.width,FLT_MAX)].height);
if (toConstant  >  currentTextViewHeight)
{
    [_hostTableView beginUpdates];
    [_hostTableView endUpdates];
}

然后在viewController中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

textCell.hostTableView = self.tableView;

以上是来客网为你收集整理的ios – 在自动调整TextView中输入文本时,UITableView闪烁/停顿全部内容,希望文章能够帮你解决ios – 在自动调整TextView中输入文本时,UITableView闪烁/停顿所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。