Self sizing UITableViewCells

Discussion in 'iOS Programming' started by John Pex, Oct 12, 2015.

  1. John Pex, Oct 12, 2015
    Last edited: Oct 12, 2015

    John Pex macrumors newbie

    Oct 12, 2015

    I have been trying to develop an app which contains self sizing Table View Cells. I've looked a lot into tutorials and online documentation and came across these two lines of code:
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension (tried 44 here also)
    The cells would work as following (see attachment):
    The user can write something and choose whether to upload or not an image. The cell would resize according to whether there is in fact an image or not and also according to the text size.
    My problems are:
    - I am using a label to hold the text, already set to 0 lines. But I cannot put de text aligned to top left. There is a limit to what the user can write;
    - The UIImageView that holds the image is also on a fixed size and the image is set on aspect to fit (I could not come up with a better solution);
    - The code that I've shown above didn't do anything. Should I uncheck something or do something within the cell properties?
    - How should I set my constraints? Of both the label and the UIImageView?

    Attached Files:

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
  3. JWalker1995 macrumors regular

    Mar 5, 2011
    the best way i have seen is with a singleton of the cell, and in the heightforrowatindexpath method, call a custom method which styles the cell and then returns myCell.frame.size.height.

    in your case, when the user has finished editing the cell (did resign first responder) just call to update cells, which in turn will call the heightforrowatindexpath method
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    @JWalker, the whole point of self-sizing tableview cells is so you don't have to implement heightForRowAtIndexPath. If things are set up correctly the table will calculate the height of the row based on its content.
  5. bjet767 macrumors 6502a

    Oct 2, 2010
    I'm not sure if I can help, but you do remember the UITableViewCell inherits from UIView and therefore you can dynamically size it. But, you would have to probably do some other dynamic changes along with the cell (as you seem to notice with the number of rows issue).

    Also why would you not want to limit the size of the image to a pre-desired size?

    It would also seem you are using the Table View cell as a view where the user can enter data via a textview. Do you want the textview to expand as the data is entered or just remain scrollable? One can use any size font the other will need to get the font dimensions and dynamically resize the view to fit without scrolling. Personally, I believe that exceeds the KISS idea a bit too much.

    What I personally do is take a building approach in solving each part of the problem. For instance I would solve the text box entry first and then add the dynamic image view.

    Have fun solving the challenge.

Share This Page

4 October 12, 2015