0

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

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).

enter image description here

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.

  • 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.

  • 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..

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.

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 :

enter image description here

If I change the estimatedRowHeight it made this worst.

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.

Bram'in
  • 618
  • 1
  • 7
  • 18

1 Answers1

1

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

You can just do this way :

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
    }

}
Marco Castano
  • 1,114
  • 1
  • 10
  • 25
  • Dude thank you ! My problem is not completely solved (error in calculating height for cell) and i think there are some little things to correct but the effect dissapear and it's really cooool ! I'll tell you soon if it works. – Bram'in Jun 16 '16 at 01:00
  • Man I have a probleme with multiline label. Do you have a solution to make the label height fit to its content ? – Bram'in Jun 16 '16 at 10:07
  • Man sorry if I write you just now, but yes i got a solution for you : yourCell.yourlabel?.numberOfLines = 0; yourCell.yourlabel?.sizeToFit(); – Marco Castano Jun 16 '16 at 18:21
  • Thank you, I've just found out this solution. Cached the height was a real good idea and I succeed in what I want. Finally it works, thank you !!! – Bram'in Jun 16 '16 at 20:43