Frage

CustomCell.h

@interface CustomCell : UITableViewCell
//Properties created in Code, not via IB.
@property (nonatomic, strong) UILabel *labelUsername;
@property (nonatomic, strong) UIView *circle;

//Properties Created through IB by control+drag
@property (nonatomic, strong) UILabel *labelFirstName;
@property (nonatomic, strong) UILabel *labelLastName;


@end

CustomCell.m

@implementation CustomCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //Creating properties in code
        self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
        [self.circle setBackgroundColor:[UIColor brownColor];

        self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
        self.labelUsername.textColor = [UIColor blackColor];

        [self.contentView addSubview:self.labelUsername];
        [self.contentView addSubview:self.circle];
    }
    return self;
}

tableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *customCellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inviteCellIdentifier];
    }

    cell.labelFirstName.text = @"FirstName";
    cell.labelLastName.text = @"LastName";
    return cell;
}

The code above continued to show labelUsername and circle. However, the properties created using the IB (labelFirstName and labelLastName) did not appear.

So in tableView.m in viewDidLoad I registered the Nib with the following code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
                                               bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"CustomCell"];
}

Now labelFirstName and labelLastName appear, but the properties created with code (labelUsername and circle) DO NOT appear.

How can I get all 4 properties to show?

War es hilfreich?

Lösung

Try this in CustomCell.m,

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
    [self.circle setBackgroundColor:[UIColor brownColor];

    self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
    self.labelUsername.textColor = [UIColor blackColor];

    [self.contentView addSubview:self.labelUsername];
    [self.contentView addSubview:self.circle];
}

Andere Tipps

Try this after self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"yourNibName" owner:self options:NULL];
[self addSubview:[views lastObject]];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top