2

I want to sort UITableViewCell's from bottom to top in UITableView. Is there a way to do that in Swift? Here is another question like this in Obj-C;

How to position UITableViewCell at bottom of screen?

I tried the @replman's answer but it didn't work well.

Anyway, I think I should explain more detailed. Let say we have a UITableView which has 1 cell. When we run, the cell shows up at the bottom of the screen. When we add second cell, then second cell slides up at the bottom of the tableView and first cell is just above it.Just like whatsapp messenger.

Here is a nice visual that I took from the question above.

+----------------+     +----------------+     +----------------+
|                |     |                |     |                |
|                |     |                |     |                |
|                |     |                |     |                |
|                |     |                |     | +------------+ |
|                |     |                |     | |   cell 1   | |
|                |     |                |     | +------------+ |
|                |     | +------------+ |     | +------------+ |
|                |     | |   cell 1   | |     | |   cell 2   | |
|                |     | +------------+ |     | +------------+ |
| +------------+ |     | +------------+ |     | +------------+ |
| |   cell 1   | |     | |   cell 2   | |     | |   cell 3   | |
| +------------+ |     | +------------+ |     | +------------+ |
+----------------+     +----------------+     +----------------+

I would be glad if someone help.

SOLUTION:

First, flip tableView and tableViewCell.Then load cells in reverse. It worked fine for me. For a little bit more information, check out this answer; https://stackoverflow.com/a/28435207/1404324

Community
  • 1
  • 1
Faruk
  • 1,669
  • 19
  • 33
  • what's wrong with the solution on the link? – Lucho Dec 19 '15 at 18:36
  • there are some issues with paddings and when the total cell heights are shorter than `tableView`'s height, the new cell just shows up. But when it doesn't new cell slides up from bottom edge of `tableView`. – Faruk Dec 19 '15 at 18:40

1 Answers1

1

There are probably many, many ways to achieve this, including:

  • add a row (or even a section) before the first one. Set it to an empty cell, and adjust its size whenever you add/remove/change rows or the layout is updated

  • same thing with a tableHeaderView (you'll need to reassign to tableHeaderView to force the tableview to notice the change of size)

  • same thing with a section header view

  • same thing with blank space inside your first cell, at the top

  • don't use a tableview but a scrollview on which you add the messages, and adjust scroll insets/offsets any way you want

There are certainly many more.

jcaron
  • 16,007
  • 5
  • 28
  • 43
  • I understand, the ways you suggested a little bit tricky:) But what do you mean "don't use a tableview but a scrollview on which you add the messages" exactly, can you explain it? Thanks. – Faruk Dec 19 '15 at 18:34
  • @Faruk well, just what it says: don't use a tableview, but just a scroll view (which will give you the scrolling behaviour you're looking for) and then add the contents in that view, handling all the layout etc. This gives you ultimate flexibility, but of course if you end up with a large number of rows, you have the issue of all the optimisations that a tableview brings that you will have to reproduce yourself. – jcaron Dec 19 '15 at 22:14
  • Actually, I asked this question because I am developing a simple chat interface. Which one do you suggest for a message view? – Faruk Dec 19 '15 at 23:19
  • I've done it in the past with a simple scroll view where I "manually" add the new messages, with constraints between them and/or the top/bottom edges, but obviously you could use a tableview. Not sure any one option is really better than the others. The scroll view option is probably the one that gives you the most control, but that also means probably a bit more work. – jcaron Dec 20 '15 at 09:36
  • I'd like to have control. Thank you for your help:) – Faruk Dec 20 '15 at 09:40
  • NB: if you're using Core Data to store your messages, using a tableview has the advantage that an `NSFetchedResultsController` will handle most of the data management and updates for you, so this would be a strong reason to use a tableview rather than a scroll view. If you don't, then it's probably a lot more balanced. It also depends on how long the list of messages can be. A tableview will handle adding visible/removing invisible rows for you to save memory and get better performance, while a scroll view will not do that by itself. YMMV... – jcaron Dec 20 '15 at 09:47