objective-c – 为什么initWithCoder不能正确地初始化项目?
发布时间:2020-12-15 01:58:49 所属栏目:百科 来源:网络整理
导读:我有一个UITableViewCell的子类。我的子类包含几个UILabels。我希望我的子类将这些标签初始化为适用于我的每个表视图单元格的一些默认设置。 我读到我应该使用initWithCoder为此。我的initWithCoder函数正在被调用,我可以通过我的函数中的每一行,看起来通
我有一个UITableViewCell的子类。我的子类包含几个UILabels。我希望我的子类将这些标签初始化为适用于我的每个表视图单元格的一些默认设置。
我读到我应该使用initWithCoder为此。我的initWithCoder函数正在被调用,我可以通过我的函数中的每一行,看起来通过动作。当我运行我的应用程序时,我看不到任何正在应用的属性。最明显的是,字体没有改变。我只是不认为我在UILabels上修改的任何属性实际上被保存或显示。 我将这个子类与故事板一起使用。我知道我的更改不会反映在故事板上,但是当应用程序运行时,它们也不会被反映出来 – 尽管我的代码被执行。 编辑:我想提到,在尝试覆盖initWithCoder之前,我在这些子类中有一个实例方法,我将运行这个逻辑。我将在cellForRowAtIndexPath内调用该实例方法。这个方法是有效的,但是我认为在这个子类中有这个逻辑自动发生会很方便。 有任何想法吗?我的代码如下: #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 和标题: #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 解决方法
当您收到消息时,您的IBOutlet都不会被设置 – 您需要使用“awakeFromNib”或“viewDidLoad” – 这样才能访问您的网点。也就是说,您可以在initWithCoder中设置其他非插件。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |