为什么initWithCoder没有正确初始化项目? [英] Why isn't initWithCoder initializing items correctly?

查看:115
本文介绍了为什么initWithCoder没有正确初始化项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewCell 的子类。我的子类包含几个 UILabel 。我希望我的子类将这些标签初始化为适用于我的每个表视图单元格的某些默认设置。

I've got a subclass of UITableViewCell. My subclass contains several UILabels. I want my subclass to initialize these labels to some default settings that applies to every table view cell of mine.

我读到我应该使用 initWithCoder 为此。我的 initWithCoder 函数正在被调用,我可以逐步浏览我的函数中的每一行,它似乎经历了这些动作。当我运行我的应用程序时,我没有看到任何属性被应用。最值得注意的是,字体没有被更改。我只是不认为我在 UILabel 上修改的任何属性实际上都被保存或显示。

I read that I should be using initWithCoder for this. My initWithCoder function is being called, and I can step through each line in my function and it appears to go through the motions. When I run my application I do not see any of the properties being applied, though. Most noticeably, the font is not being changed. I just don't think any of the properties that I'm modifying on my UILabels are actually being saved, or displayed.

我正在将此子类与 Storyboard 结合使用。我知道我的更改不会反映在 Storyboard 上,但是在应用程序运行时它们也没有反映出来 - 尽管我的代码正在执行。

I'm using this subclass in conjunction with a Storyboard. I know my changes will not be reflected on the Storyboard, but they're also not being reflected when the application runs - despite my code being executed.

编辑:我想在尝试覆盖 initWithCoder 之前提一下,我在这些子类中有一个实例方法,我运行这个逻辑输入。我只是在 cellForRowAtIndexPath 中调用该实例方法。这个方法很有效,但我认为在这个子类中自动生成这个逻辑会很方便。

I wanted to mention that prior to trying to override initWithCoder, I had an instance method in these subclasses that I'd run this logic in. I would just call that instance method inside of cellForRowAtIndexPath. This method was working, but I thought it'd be handy to have this logic in this subclass occur automatically.

有什么想法吗?我的代码如下:

Any ideas? My code is below:

#import "ThreadListCell.h"

@implementation ThreadListCell

- (id)initWithCoder:(NSCoder *)aDecoder {

    if ((self = [super initWithCoder:aDecoder])) {

        // adjust the font settings of the title
        self.title.font = [UIFont fontWithName:@"SourceSansPro-Black" size:16];
        self.title.textColor = [UIColor colorWithWhite:0.267f alpha:1.0f];

        // adjust the font settings of the subtitle
        self.text.font = [UIFont fontWithName:@"SourceSansPro-Light" size:14];
        self.text.textColor = [UIColor colorWithWhite:0.267f alpha:0.9f];
        self.text.textColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];

        // adjust the font settings of the location
        self.location.font = [UIFont fontWithName:@"SourceSansPro-Light" size:8];
        self.location.textColor = [UIColor colorWithWhite:0.267f alpha:0.9f];

        // adjust the UILabel settings of the title
        self.title.numberOfLines = 0;
        self.title.lineBreakMode = UILineBreakModeTailTruncation;

        // adjust the UILabel settings of the subtitle
        self.text.numberOfLines = 0;
        self.text.lineBreakMode = UILineBreakModeTailTruncation;

        // adjust the UILabel settings of the location
        self.location.numberOfLines = 0;
        self.location.lineBreakMode = UILineBreakModeTailTruncation;
        self.location.textAlignment = UITextAlignmentRight;

    }

    return self;

}

@end

header:

#import <UIKit/UIKit.h>

@interface ThreadListCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *text;
@property (nonatomic, weak) IBOutlet UILabel *title;
@property (nonatomic, weak) IBOutlet UILabel *location;

@end


推荐答案

无当您收到消息时,您的IBOutlets会被设置 - 您需要使用awakeFromNib或viewDidLoad - 类似的东西 - 来访问您的商店。也就是说,您可以在initWithCoder中设置其他非插座内容。

None of your IBOutlets is set when you get the message - you need to use "awakeFromNib" or "viewDidLoad" - something like that - to access your outlets. That said, you can set other non-outlet stuff in the initWithCoder.

这篇关于为什么initWithCoder没有正确初始化项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆