UITableViewCell , UILabel and UIButton are all subclasses of UIView , and the documentation for UIView says:
Manage layouts and subheadings
- A view may contain zero or more views.
- Each view defines its own resizing behavior in relation to its parent view.
- A view can manually resize and position its subzones as needed.
Thus, it is certainly possible to do so.
You can create your own labels and buttons using initWithFrame: with the CGRectZero argument, and then resize them (based on text or whatever) using setBounds: or setFrame: (because right now you are just going to set the size of the view). Then add these subviews as subzones of the contentView cell.
Then in a custom subclass of UITableViewCell you can implement your solution by overriding the default behavior (which does nothing) from layoutSubviews: to set the subframe source field (i.e. CGRect) that will position the subviews in the cell content view (the size is already set ) You may need to call setNeedsLayout: or layoutIfNeeded:
This is a really rough outline of how the solution can be implemented, because there are many details left. For example, if you change the size of a button based on the titleLabel text, you probably want to titleLabel some in width and height, otherwise the button will be the size of the label and will look strange. In the layoutSubviews: method, layoutSubviews: may be enough logic to layout labels and buttons the way you want (for example, it would be easier if all the subzones of the cell, where the same type as all the labels) esp. if subviews can be wrapped to a new line.
yabada
source share