我已經有了一個UITableViewCell
一個子類。我的子類包含幾個UILabel
s。我希望我的子類將這些標籤初始化爲適用於我的每個表視圖單元的一些默認設置。爲什麼initWithCoder不正確初始化項目?
我讀,我應該使用initWithCoder
這一點。我的initWithCoder
函數正在調用中,我可以遍歷函數中的每一行,並且它似乎貫穿了運動。但是,當我運行我的應用程序時,我看不到任何應用的屬性。最明顯的是,字體沒有被改變。我只是不認爲任何我修改我的UILabel
S中的屬性實際上被保存,或顯示。
我使用與Storyboard
結合這個子類。我知道我的變化不會對Storyboard
反映,但他們在應用程序運行也沒有體現 - 儘管正在執行我的代碼。
編輯:我想提一提,試圖重寫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
哦,好。用'awakeFromNib'代替'initWithCoder'似乎可以正常工作。非常感謝! – Ryan 2012-08-13 00:25:10
儘管在'cellForRowAtIndexPath'中設置了我的任何屬性之前,它看起來像是'awakeFromNib'。這使我認爲或許我的實例方法是更好的方法,因爲我可以可靠地根據「UILabel」中的當前文本執行大小調整操作等。 – Ryan 2012-08-13 00:44:02
順序應該是:initWithCoder,awakeFromNib,cellForRowAtIndexPath。如果你需要「預置」一些數學值,你可以在init例程中完成。但是,在筆尖加載之前,您無法訪問您的網點。 – 2012-08-13 01:01:29