3

I'm rather new to Objective-C programming and I'm making an app that requires the Table View to load from the bottom to the top, similar to messaging apps. I've tried searching on Google about this but I'm still a bit confused about the code used. Thanks in advance!

Wain
  • 118,658
  • 15
  • 128
  • 151
Paco Wong
  • 645
  • 7
  • 14

3 Answers3

0

UITableView polls its dataSource for cells based on what is visible on the screen. So, the key to your problem is you want to show the bottom of the table first.

In order to do that, you can call [tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];, which will scroll the UITableView to the bottom.

Keep in mind that where you place that call may vary based on whether or not you are using Autolayout.

See How to scroll to the bottom of a UITableView on the iPhone before the view appears for more information

Community
  • 1
  • 1
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • 5
    I don't understand how this answers the question. the person requested a list like the messaging application where items begin at the bottom of the screen. – zwebie Mar 16 '16 at 18:48
0

Here's what I did when I designed a chatbot:

1 - Add a UItableView in IB and set its constraints.

2 - When I added a new cell (message), I would change the Y position of the UITableView. For example, when the first message was added to the data source array, I would calculate the height of that cell, so if the height of that cell was 50, I would set the Y position of my UITableView as the current height of the UITableView minus the height of the cell that is being added. This makes it look like the messages are being added from the bottom.

3 - Repeat the above cycle in step 2, until the Y position of your UITableView is just below the top of the screen (or the required position), in that case you no longer have to set the subtracted value as the Y position (otherwise the tableview would go off screen). Add a if condition that checks if the calculated values is lower than the required position, if it is then just set the Y position yourself (So the table is pinned at the top).

4 - Also make sure that you always scroll to the last cell of your UITableView using scrollToRowAtIndexPath.

Hope this helps.

Tejas K
  • 682
  • 11
  • 28
0

An alternative way to solve this is by flipping UITableView’s Y axis

taken from: Igor Kulman

The best solution I found is to flip the UITableView’s Y axis. It gives you all the advantages of the previous solution, but you do not have to handle the scrollbar. It stays in the right place because only the content is flipped. So flip the UITableView

tableView.transform = CGAffineTransform(scaleX: 1, y: -1)

and flip your cells in the same way

cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
Andy
  • 4,441
  • 1
  • 19
  • 16