UITableView在自动调整TextView大小时输入文本时闪烁/停顿 [英] UITableView flickers/stutters while entering text in auto resizing TextView

查看:174
本文介绍了UITableView在自动调整TextView大小时输入文本时闪烁/停顿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个UITableView,其中的大小为UITextView.使用beginUpdates/endUpdates可以自动调整单元格的大小,但是当它执行时,表视图会变得混乱(请参见下面的gif).

Currently I have a UITableView with a resizing UITextView in it. The cell is resizing automatically using beginUpdates/endUpdates, but when it does it the table view stutters (See the gif below).

最终结果是一个UITableViewCell,其中包含一个textview,该textview根据其内容调整大小.这是自定义UITableViewCell类中的代码,该代码使UITableView自行更新.

The end result is a UITableViewCell that has a textview in it that resizes based on it's content. Here is the code within the custom UITableViewCell class that causes the UITableView to update itself.

- (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];
    }
}

这是我已经尝试过的东西:

Here are the things that I have already tried:

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

Get the current contentOffset and resetting it after the endUpdates but didn't work Disabling scrolling on the UITableView before updates and then enabling afterwards I tried returning NO always from - (BOOL)textViewShouldEndEditing:(UITextView *)textView My UITableView cell height is using UITableViewAutomaticDimension. Any other ideas or thoughts are welcome.

以下是其外观示例:

我不想使用任何库,因此请不要提供任何建议.

I am not looking to use any libraries so please no suggestions for that.

谢谢

解决方案

在这里找到:向@JeffBowen致信,以获取一个不错的发现(尽管它很可行,但允许我仍然实现UITableViewDelegate方法来支持iOS 7).在执行更新之前关闭动画,然后在更新之后启用动画,以防止UITableView停顿.

Credit to @JeffBowen for a great find (although hacky it is workable and allows me to still implement the UITableViewDelegate methods for supporting iOS 7). Turn animations off prior to performing update and then enable after update to prevent the UITableView from stuttering.

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

如果您不需要使用Delegate方法,并且只希望针对iOS 8+的要求不高的解决方案,那么请使用下面的@Massmaker答案.

If you don't need to use the Delegate methods and want a less hacky solution for iOS 8+ only then go with @Massmaker's answer below.

推荐答案

我的解决方案(适用于iOS 8)是首先在viewController viewDidLoad

My solution (for iOS 8) was first set in my 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;

然后,结合本文 Swift中的解决方案和一些肮脏的想法 我在单元格中设置了一个名为

then, combining this article solution in Swift and some dirty thoughts I`ve set in my cell a property, called

@property (nonatomic, strong) UITableView *hostTableView;

和单元格-(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中

then in viewController

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

textCell.hostTableView = self.tableView;

这篇关于UITableView在自动调整TextView大小时输入文本时闪烁/停顿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆