how to create a "container" view of a row of UILabels?
Can I ask for some high level guidance regarding how to create a "container" type view for a row of 3 UILabels, to be used within a custom sub-classed UITableViewCell? How would you do this, which classes would you sub-class for this for example? How many methods would I need to override for this? (e.g. layoutSubviews, other?)
BACKGROUND:
- I already have a custom sub-classed UITableViewCell where I'm displaying multiple rows of UILabels ok, with each row there are 3 UILabels.
- to make the code look cleaner it would be good to treat t开发者_如何学JAVAhe "row of 3 x UILabels" as one view so to speak
- how would I do this - would I just sub-class "UIView" perhaps for this? then I would override "layoutSubviews" in this so get the new "row of 3 x UILabels" laying themselves out.
Which approach to use:
i would like to go with your second option rather than others,
because if you separate out 3 UILabel
from UITableViewCell inherited custom cell class and put them into a UIView inherited class.
1.The code will look cleaner (especially your Custom cell class). As pointed by your.
2.Further if you require more than 3 UILabels or any UIView inherited view, No changes is required in your custom cell class,
How would I do this
Create a UIView
inherited class, add all your UILabels
as subviews then provide the implementation to -(void) layoutSubviews
, set the frame for each UILabel's.
Add subclassed UIView as subview in your custom cell and set frame for it in -(void) layoutSubviews
method,
You would either subclass UIView or put everything in a view controller. i would pick the 2nd option since you're not requiring your custom view to do complex stuff, a subclass seems a bit "too much" for this purpose.
Make some instance variables in the view controller (In the .h):
UILabel *label1, *label2, *label3;
Then in your implémentation (in the .m), do the following: 'viewDidLoad' method:
// First label... Create and configure.
label1 = [[UILabel alloc] init];
// [Configure here]
[[self view] label1];
// Second label... Create and configure.
label2 = [[UILabel alloc] init];
// [Configure here]
[[self view] label2];
// Third label... Create and configure.
label3 = [[UILabel alloc] init];
// [Configure here]
[[self view] label3];
Then in 'viewDidLoad' method:
// Release / Nillify everything.
[label1 release], label1 = nil;
[label2 release], label2 = nil;
[label3 release], label3 = nil;
In your 'viewDidAppear' and any other method signaling an orientation change, you have then to configure your layout... Use the parent view rect to get the bounds / frame, and change the 3 labels geometry according to your needed layout.
If you prefer to do it by subclassing UIView, you can do exactly the same, but instead of doing the layout in viewDidLoad, you'll have to do it in 'layoutSubviews' as you planned. Also, initialization / allocation code may go to 'initWithCoder:' if your view is from a XIB, and deallocation in your dealloc.
Hope that helps, Pierre.
精彩评论