开发者

Create a custom UIView as a tableHeaderView programmatically

开发者 https://www.devze.com 2023-01-23 17:47 出处:网络
I want to create a \'detail view\' in my navigation-based app similar to the address book app. I want to create a UIView that has an UIImageView开发者_如何学C and a UILabel that I can pass to a UITa

I want to create a 'detail view' in my navigation-based app similar to the address book app.

I want to create a UIView that has an UIImageView开发者_如何学C and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.

I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = @"some new text". It just kept presenting the text I entered in the IB inspector window.

So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?

Should I be creating a new class that is a sub-class of UIViewController or just UIView?

How do I go about creating the sub-views for the label and image?

Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?


If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.

Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.

But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).

And I would override initWithFrame and do all the setup there.

Something like the following:

myView.m

@implementation myView

@synthesize imageView, myLabel;

- (id) initWithFrame:(CGRect) frame
{
    if (self = [super initWithFrame:frame]) {

        // Setup your image view and add it to the view.
        self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
        self.imageView.image = ...
        [self addSubview:self.imageView];

        // Setup your label
        self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
        self.myLabel.text = @"whatever you like";
        [self addSubview:self.myLabel];
    }
    return self;
}

Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消