Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

John Pex

macrumors newbie
Original poster
Oct 12, 2015
9
0
Hello,

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:
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?
 

Attachments

  • Screen Shot 2015-10-12 at 11.47.32 PM.png
    Screen Shot 2015-10-12 at 11.47.32 PM.png
    87.1 KB · Views: 148
Last edited:
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
 
@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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.