
所以我有一个自定义UItableVIEwCell(称为CustomCell),它有几个属性,其中一个是我自己的标签.因为我从故事板加载单元格,我用initWithCoder初始化它而不是initWithStyle:reuseIDentifIEr:,这就是我在CustomCell.m中所拥有的,它是UItableVIEwCell的子类(由于某种原因,我无法弄清楚如何使用故事板设置自定义字体..但这不是这个问题的重点):
// CustomCell.m - subclass of UItableVIEwCell- (ID)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { NSLog(@"customizing cell Font having text %@",self.label.text); UIFont *customFont = [UIFont FontWithname:@"Montserrat" size:16]; self.label.textcolor = [UIcolor redcolor]; [self.label setFont:customFont]; } return self;} 这根本不起作用..日志语句为文本输出null,只是b / c尚未加载文本. self.label也是null(我不知道为什么我认为它现在应该从nib中膨胀)但是即使我在这里初始化它仍然无效.
所以我的工作是简单地将这个单元格自定义部分放在tableVIEwController中:
- (voID)tableVIEw:(UItableVIEw *)tableVIEw willdisplayCell:(UItableVIEwCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { UIFont *customFont = [UIFont FontWithname:@"Montserrat" size:16]; [((CustomCell *)cell).label setFont:customFont];} 工作得很好..我对这种方法不满意,我想知道如何在CustomCell.m中使它工作
更新:使事情变得更有趣..如果我在initWithCoder中放置UItableVIEwCell属性的自定义代码,它们可以工作!考虑这个例子:
- (ID)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { UIVIEw *bgcolorVIEw = [[UIVIEw alloc] init]; [bgcolorVIEw setBackgroundcolor:[UIcolor bluecolor]]; [self setSelectedBackgroundVIEw:bgcolorVIEw]; // actually works! } return self;} 这使得这更加奇怪.
解决方法 受到 andykkt评论的启发,我在 awakeFromNib文档中找到了解释:The nib-loading infrastructure sends an awakeFromNib message to each
object recreated from a nib archive,but only after all the objects in
the archive have been loaded and initialized. When an object receives
an awakeFromNib message,it is guaranteed to have all its outlet and
action connections already established.
所以这解释了上面的奇怪行为:initWithCoder实例化一个标准的UItableVIEwCell(已经预先带有backgroundVIEw等等)但是它仍然无法识别我通过storyboard添加到它的插座.. awakeFromNib.
总结以上是内存溢出为你收集整理的ios – 为什么UITableViewCell初始化在initWithCoder中不起作用全部内容,希望文章能够帮你解决ios – 为什么UITableViewCell初始化在initWithCoder中不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)