在设置为 Autoresize 的 tableView 中插入导致滚动问题 [英] Insert in a tableView set to Autoresize cause a scroll issue

查看:24
本文介绍了在设置为 Autoresize 的 tableView 中插入导致滚动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几天的问题,我真的无法解释为什么会这样.

I've an issue for few days and really I can't explain why it goes like that.

我正在聊天,设置一个tableView,将消息打印到单元格中.这些细胞是用原型设计的,有 3 种不同的类型(但无论如何都没关系).文本输入,消息发送,单元格插入在表格中,然后我们滚动到底部表视图.

I'm doing a chat, set up with a tableView, printing message into cell. These cells are designed with prototypes, there are 3 different type (but anyway it doesn't matter). Text is typed, message is send, cell is inserted in table and then we scroll to the bottom of the tableView.

如你所知,在聊天视图中,消息的容器必须适合这个文本(这是一个视图),然后单元格必须适合这个容器(橙色标签,紫色的容器).

As you know, in a chat view the container of the message has to fit this text (which is a view), and then the cell has to fit to this container (Label in orange, container in purple).

此容器具有可变高度,并沿文本增长,改变单元格高度.

This container has a variable height and grow along the text, changing cell height.

  • 我为自动布局显示设置了许多约束,但我的单元格高度与我在项目中最初为其设置的高度没有不同(无自适应行为).聊天消息被截断.

  • I've set many constraint for auto-layout display but I didn't have different cell height than the height I initially set for it in the project (no adaptative behaviour). Chat Message were cut.

因此,我尝试使用 heightForRowAtIndexPath 方法自行设置每个 rowHeight,沿文本大小计算约束,但它会产生真正的不良行为(例如,在滚动时更改单元格).大小经常被错误计算/重新计算.

So, I've tried to set each rowHeight by myself using the method heightForRowAtIndexPath, calculating constraint along text size but it create a real bad behaviour (changing cells when scrolling for example). The size was often wrong calculated/recalculated.

这就是为什么我最终将 estimatedRowHeight 设置为 44UITableViewAutomaticDimension 结合使用.这就是诀窍!哇!但是并没有想象中的那么好..

That's why I'm finally using estimatedRowHeight set to 44 combine with UITableViewAutomaticDimension. Here it does the trick ! Waouh ! But it's not as good as expected..

当表格视图出现时,一切都很好.容器适合他们的标签,行的高度适合他们的容器,而且很漂亮.问题出现在插入过程中.经过多次测试,我注意到这种不良行为仅在 multilines label 保留在表格视图中时才会出现.

When the table view appears all is good. Containers fit to their label, rows' height fit to their container and it's beautiful. The problem appears during an insert. After many tests, I notice that this bad behaviour only appears when multilines label remains in the table view.

在插入期间,每个单元格似乎在调整其高度以适应内容之前将其大小调整为 44,然后与之前的状态相比创建了一个间隙,从而创建了表格视图的奇怪滚动:

During an insert, each cell seems to resize to 44 before adapt its height to content, and then create a gap compared to previous state which create a strange scroll of the table view :

如果我更改了 estimatedRowHeight,它会使情况变得更糟.

If I change the estimatedRowHeight it made this worst.

如果你愿意,我可以显示我的代码,但我认为它在这里不会很有用,因为我没有使用复杂的功能..只有插入,单元格的自动高度和向下滚动,这是tableView的功能委托.

I can show my code if you want, but I don't think it will be very useful here because I'm not using complicated function.. only insert, automatic height for cells and scroll down which are functions delegate for tableView.

你能帮我吗?真的我不知道如何改变它......每个聊天应用程序都可以解决问题,但我找不到方法.

Can you help me please ? Really I don't know how to change that.. every chat application does the trick but I can't found out the way.

谢谢你的回答,请原谅我的英语水平我是一个可怜的法国学生..

Thank you for answer, excuse my english level I'm a poor french student..

如果你需要一些代码注释,我会给它.

If you need some code comment I'll give it.

推荐答案

我认为你可以在 cellForRowAtIndex 方法中缓存每个单元格的高度值,并在 HeightForRowAtIndex 中设置该单元格对应的高度.

I think you can cache height value for every cell in cellForRowAtIndex method and in HeightForRowAtIndex set the corresponding height for that cell.

你可以这样做:

var cellCached = Dictionary<Int,AnyObject>()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
table.rowHeight = yourCell.yourLabel.frame.origin.y + yourCell.yourLabel.frame.height + 10

cellCached[indexPath.row] = yourTable.rowHeight
}


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if(tableView == yourTable)
    {
        if(cellCached[indexPath.row] != nil)
        {
            return cellCached[indexPath.row] as! CGFloat
        }
        else
        {
            return 200
        }
    }
    else
    {
        return UITableViewAutomaticDimension
    }

}

这篇关于在设置为 Autoresize 的 tableView 中插入导致滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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