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

DMC*_*pps 17 uitableview ios

目前我有一个UITableView调整大小UITextView.单元格使用beginUpdates/ 自动调整大小endUpdates,但是当它执行时,表格视图会断断续续(请参阅下面的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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

以下是它的样子:

表视图口吃

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

谢谢

编辑:解决方案

在这里找到:我不希望动画在开始更新,结束更新块为uitableview?

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

[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

如果您不需要使用Delegate方法并且只想要一个不那么hacky的iOS 8+解决方案,那么请使用@ Massmaker的答案.

Mas*_*ker 1

我的解决方案(适用于 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;
Run Code Online (Sandbox Code Playgroud)

然后,结合Swift 中的这篇文章解决方案 和一些肮脏的想法,我在单元格中设置了一个属性,称为

@property (nonatomic, strong) UITableView *hostTableView;
Run Code Online (Sandbox Code Playgroud)

并在细胞-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];
}
Run Code Online (Sandbox Code Playgroud)

然后在viewController中

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

textCell.hostTableView = self.tableView;
Run Code Online (Sandbox Code Playgroud)

高度改变后