有没有任何方法刷新单元格的高度没有重载/ reloadRow? [英] Is there any way refresh cell's height without reload/reloadRow?

查看:150
本文介绍了有没有任何方法刷新单元格的高度没有重载/ reloadRow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个像imessage的视图,只需在底部文本视图中输入文本。我使用表视图来执行此操作,并使用最后一个单元格中的文本视图。当我输入多行的长文本时,我需要文本视图,单元格变成了尾部。所以我需要刷新单元格的高度。但如果我使用表视图的重新加载或重新加载行,文本视图中的内容将消失,键盘也将消失。有没有更好的方法来解决它?

I make a view like imessage, just input text into the bottom text view. I use table view to do this, and the text view in the last cell. when I input long text that more than one line, I need the text view and the cell become tailer. so I need refresh cell's height. but if I use table view's reload or reload row, the content in text view will disappear and the keyboard will disappear too. Is there any way better to fix it?

可能我应该使用工具栏来轻松完成吗?但我仍然怀疑表视图可以做到。

May be I should use tool bar to do it easy? but I still doubt table view can do it.

推荐答案

当您调用 beginUpdates时,单元格将平滑调整大小 endUpdates 。在这些调用之后,tableView将为表中的所有单元格发送 tableView:heightForRowAtIndexPath:,当tableView获得所有单元格的所有高度时,它将为调整大小设置动画。

The cells will resize smoothly when you call beginUpdates and endUpdates. After those calls the tableView will send tableView:heightForRowAtIndexPath: for all the cells in the table, when the tableView got all the heights for all the cells it will animate the resizing.

您可以通过直接设置单元格的属性来更新单元格而无需重新加载它们。不需要涉及tableView和 tableView:cellForRowAtIndexPath:

And you can update cells without reloading them by setting the properties of the cell directly. There is no need to involve the tableView and tableView:cellForRowAtIndexPath:

要调整单元格的大小,您将使用类似的代码到这个

To resize the cells you would use code similar to this

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
    CGSize size = // calculate size of new text
    if ((NSInteger)size.height != (NSInteger)[self tableView:nil heightForRowAtIndexPath:nil]) {
        // if new size is different to old size resize cells. 
        // since beginUpdate/endUpdates calls tableView:heightForRowAtIndexPath: for all cells in the table this should only be done when really necessary.
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    }
    return YES;
}

更改单元格的内容而不重新加载我使用如下内容:

to change the content of a cell without reloading I use something like this:

- (void)configureCell:(FancyCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    MyFancyObject *object = ...
    cell.textView.text = object.text;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FancyCell *cell = (FancyCell *)[tableView dequeueReusableCellWithIdentifier:@"CellWithTextView"];
    [self configureCell:cell forRowAtIndexPath:indexPath];
    return cell;
}

// whenever you want to change the cell content use something like this:

    NSIndexPath *indexPath = ...
    FancyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [self configureCell:cell forRowAtIndexPath:indexPath];

这篇关于有没有任何方法刷新单元格的高度没有重载/ reloadRow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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