以编程方式创建自定义 UIView 作为 tableHeaderView

发布于 2024-10-01 05:07:00 字数 651 浏览 2 评论 0原文

我想在基于导航的应用程序中创建一个类似于地址簿应用程序的“详细视图”。

我想创建一个 UIView ,它有一个 UIImageView 和一个 UILabel ,我可以将其传递给 UITableVIew 的由导航控制器推送时的 tableHeaderView 属性。标签文本和图像在加载时必须从托管对象上下文中获取信息。

我开始尝试使用 IB 来执行此操作,但是当标签文本无法更新时,无论我将 myView.UILabel.text = @"some new text" 放在哪里,我都会感到厌倦。它只是一直显示我在 IB 检查器窗口中输入的文本。

因此,如果不使用 IB,我将如何创建一个包含 UILabelUIImageViewUIView 呢?

我应该创建一个新类作为 UIViewController 的子类还是只是 UIView 的子类?

如何为标签和图像创建子视图?

文本和图像 URL 应该在代码中的哪里设置? (viewDidLoad 或 loadView 或 viewWillLoad)(UIView,UIViewController,detailViewController)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-10-08 05:07:00

如果您开始使用 IB 并且 UILabel 文本不会更新,听起来您的视图控制器的 IBOutlet 未正确连接到 IB 中的标签。

希望这能解决您使用 IB 的问题。在 viewController 代码中,您应该更新 viewDidLoad 中的文本。

但如果你想以编程方式执行此操作,我将创建 UIView 的子类(而不是 UIViewController)。

我会覆盖 initWithFrame 并在那里完成所有设置。

如下所示:

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;
}

确保在 dealloc 方法中正确清理内存,当然这取决于您是否喜欢 make 类属性还是类变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文