4

I have an array of HTML strings, some of them are with photos, some of them long, some short, some with links - they are all different.

Now I have a UITableView and I want to display those HTML strings on the table, each string in each cell by it's index. My problem is that I don't know the height of each cell, I need to calculate the height by the height of each HTML string, but I can only know it after the UIWebView is loaded.

I need a suggestion for a good solution to this issue, something that will looks good. I've already tried to use UIWebViewDelegate to change the cell frame after loading the HTML string into the WebView but it looks bad.. and messes the table view completely.

Thanks in advance.

ytpm
  • 4,962
  • 6
  • 56
  • 113
  • you can refer to this answer http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – pqteru Jul 06 '14 at 17:04

1 Answers1

1

Resort to animation when adding the cells.

Add the cells one by one (1 indexPath at a time) in your UITableView (and datasource), by doing this:

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
                          // or UITableViewRowAnimationNone
    [self.tableView endUpdates];

Your UITableView will slowly grow with an animation as the data becomes available.

If you need to resize an existing cell, delete it and immediately re-insert it, all within a single beginUpdates/endUpdates sequence:

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView insertRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • I accepted your answer while understanding that there is no 'smooth' solution to this question. There is no way to add `UIWebView` in `UITableViewCell` that's gonna work smooth and clean. – ytpm Jul 13 '14 at 13:21
  • 1
    Don't give up! If the size of a `UITableViewCell` changes after the fact (*say you started with 10 pix tall and now you need 20*) you can actually use the method I described above: **(1)** beginUpdates to freeze the UI **(2)** delete and re-insert right away the very row you are resizing ( `-deleteRowsAtIndexPaths:withRowAnimation:` + `-insertRowsAtIndexPaths:withRowAnimation:`) **(3)** endUpdates to release the UI. This will be smooth. – SwiftArchitect Jul 13 '14 at 20:54
  • Yeah i've used it before, I can just reload the cell I want. But it's not good to me, because I want the table to load smoothly and this is massy... But thanks anyway! I don't have 1-3 cells, I have 50-60 cells... – ytpm Jul 14 '14 at 08:40
  • One possibly cool way to do this is use animation scheduling: make a queue of animations where the `^{completion}` method of one triggers the next one. If you schedule each animation within 1/15th secs of each other, you could load your table withing about 4 seconds, slowly growing. Note that at worse the user can only see a dozen cells on the screen at once, so once you are past the visible part, animation is possibly of little value – SwiftArchitect Jul 14 '14 at 21:42