在 initWithCoder 中设置自定义 UITableViewCell 样式:不起作用 [英] Styling custom UITableViewCell in initWithCoder: not working

查看:28
本文介绍了在 initWithCoder 中设置自定义 UITableViewCell 样式:不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 UITableViewCell 以及如何使用故事板管理事物方面遇到了一些问题.当我将样式代码放在 initWithCoder: 中时,它不起作用,但如果我将它放在 tableView: cellForRowAtIndexPath: 中,它会起作用.在故事板中,我有一个原型单元格,其类属性设置为我的 UITableViewCell 自定义类.现在 initWithCoder: 中的代码确实被调用了.

I have some issues with a custom UITableViewCell and how to manage things using storyboards. When I put the styling code in initWithCoder: it doesn't work but if I put it in tableView: cellForRowAtIndexPath: it works. In storyboard I have a prototype cell with its class attribute set to my UITableViewCell custom class. Now the code in initWithCoder: does get called.

SimoTableViewCell.m

@implementation SimoTableViewCell

@synthesize mainLabel, subLabel;

-(id) initWithCoder:(NSCoder *)aDecoder {
    if ( !(self = [super initWithCoder:aDecoder]) ) return nil;

    [self styleCellBackground];
    //style the labels
    [self.mainLabel styleMainLabel];
    [self.subLabel styleSubLabel];

    return self;
}

@end

TableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NearbyLandmarksCell";
    SimoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //sets the text of the labels
    id<SimoListItem> item = (id<SimoListItem>) [self.places objectAtIndex:[indexPath row]];
    cell.mainLabel.text = [item mainString];
    cell.subLabel.text = [item subString];

    //move the labels so that they are centered horizontally
    float mainXPos = (CGRectGetWidth(cell.contentView.frame)/2 -      CGRectGetWidth(cell.mainLabel.frame)/2);
    float subXPos = (CGRectGetWidth(cell.contentView.frame)/2 - CGRectGetWidth(cell.subLabel.frame)/2);
    CGRect mainFrame = cell.mainLabel.frame;
    mainFrame.origin.x = mainXPos;
    cell.mainLabel.frame = mainFrame;
    CGRect subFrame = cell.subLabel.frame;
    subFrame.origin.x = subXPos;
    cell.subLabel.frame = subFrame;

    return cell;
}

我调试了代码,发现dequeue...先被调用,然后进入initWithCoder:,再回到视图控制器代码.奇怪的是,内存中单元格的地址在 return self; 和返回控制器之间发生了变化.如果我在 dequeue... 之后将样式代码移回视图控制器,一切正常.只是我不想在重复使用单元格时做不必要的样式.

I have debugged the code and found that the dequeue... is called first, then it goes into the initWithCoder: and then back to the view controller code. What is strange is that the address of the cell in memory changes between return self; and when it goes back to the controller. And if I move the styling code back to the view controller after dequeue... everything works fine. It's just I don't want to do unnecessary styling when reusing cells.

干杯

推荐答案

在单元格上调用 initWithCoder: 后,单元格被创建并设置其属性.但是,单元上的 XIB(IBOutlets)中的关系尚未完成.所以当你尝试使用 mainLabel 时,它是一个 nil 引用.

After initWithCoder: is called on the cell, the cell is created and has its properties set. But, the relationships in the XIB (the IBOutlets) on the cell are not yet complete. So when you try to use mainLabel, it's a nil reference.

将您的样式代码移至 awakeFromNib 方法.在解压 XIB 后创建和完全配置单元后调用此方法.

Move your styling code to the awakeFromNib method instead. This method is called after the cell is both created and fully configured after unpacking the XIB.

这篇关于在 initWithCoder 中设置自定义 UITableViewCell 样式:不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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