0

免责声明:我是 iOS 开发的新手。我在这个项目中使用 ARC

我有一个非常简单的习惯UITableViewCell

UITableViewListRightAlignedCell.h

@interface UITableViewListRightAlignedCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@property (weak, nonatomic) IBOutlet UIImageView *imgIcon;

@end

UITableViewListRightAlignedCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self initalize];
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    [self initalize];
}

-(void) initalize {
    self.lblTitle.backgroundColor = [UIColor clearColor];
    self.lblTitle.textAlignment = UITextAlignmentRight;
    self.lblTitle.font = [UIFont fontWithName:FONTRobotoRegular size:16];
    self.lblTitle.textColor = RGBColor(0x5D350BFF);  
}

当我的情节提要中有一个单元格并且我连接UILabellblTitle和连接UIImage到时,此自定义单元格可以正常工作imgIcon

但是,我想在代码中的其他地方使用相同的自定义单元类,并以编程方式创建UITableView. 问题是属性lblTitleimgIcon没有被设置,因为它们没有被初始化,我无法初始化它们,因为它们是插座和weak.

我需要知道在这种情况下正确的方法是什么。

4

3 回答 3

0

你必须知道,在使用 Storyboard 时,只会调用 awakeFromNib。仅在另一个类以编程方式初始化的情况下才调用 initWithStyle。

因此,您在使用这种代码时是正确的:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self initalize];
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    [self initalize];
}

-(void) initalize {
   // initialization code  
}

这正是 Paul Hegarty 在斯坦福 iOS 课程中展示的内容。它确保无论调用者如何,您的类都以一致的方式初始化。

因此,要回答您的问题,如果您想以编程方式使用此类,请仅在 initWithStyle 方法中添加此类代码(因为它是以编程方式调用的):

_lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.contentView addSubview:_lblTitle];

你的其他财产也是如此。

当然,您不会将其添加到 awakeFromNib 方法中,因为您的 outlet 已经由情节提要本身初始化。

问候,弗雷德

于 2013-09-12T17:52:49.733 回答
0
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {

            [self createMyLabel:nil];// create your label here
            // Initialization code
            [self initalize];
        }
        return self;
    }

    -(void)awakeFromNib {
        [super awakeFromNib];
        [self initalize];
    }

    -(void) initalize {
        //self.lblTitle.backgroundColor = [UIColor clearColor];
        self.lblTitle.textAlignment = UITextAlignmentRight;
        self.lblTitle.font = [UIFont fontWithName:FONTRobotoRegular size:16];
        self.lblTitle.textColor = RGBColor(0x5D350BFF);  
    }



    -(void)createMyLabel:(id)sender{

        //Create your label
        UILabel *MyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];

        //Set background color for debugging
        [storeAddress setBackgroundColor: [UIColor blueColor]];

        //set this created label as your lblTitle label
        [self setLblTitle:MyLabel];


        //Add it to the cell's content view
        [[self contentView] addSubview:[self lblTitle]];


        //Then do something with that
        //[self initalize];

        //[MyLabel release]; // For Non ARC

    }
于 2013-09-12T12:01:34.130 回答
0

每个表格视图单元格都包含 contentView。这个视图就是你在 IB 中看到的。当您在 IB 中向它添加控件时(在您的情况下为 UILabel),您可以将它们用作弱控件,因为此 contentView 已经对其具有强引用。所以你需要什么:

UILabel *label =  [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[label setText:@"Test"];
[cell.contentView addSubview:label];
[cell setTitleLabel:label];
于 2013-09-12T12:12:13.133 回答