有没有任何方法刷新单元格的高度没有重载/ reloadRow?

Zho*_*uQi 26 tableview ios

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

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

Mat*_*uch 67

当你打电话的细胞将平稳调整beginUpdatesendUpdates.在这些调用之后,tableView将发送tableView:heightForRowAtIndexPath:表中的所有单元格,当tableView获得所有单元格的所有高度时,它将为调整大小设置动画.

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

要调整单元格的大小,您可以使用与此类似的代码

- (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;
}
Run Code Online (Sandbox Code Playgroud)

要在不重新加载的情况下更改单元格的内容,请使用以下内容:

- (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];
Run Code Online (Sandbox Code Playgroud)