bdev
bdev

Reputation: 2058

Subclassed UIView in UITableViewCell from NIB

I created a UITableViewCell in IB that contains a custom UIView. The custom UIView is also contained in a NIB. How do I load this custom UIView into the custom UITableViewCell?

Upvotes: 2

Views: 714

Answers (2)

Thiên Quang
Thiên Quang

Reputation: 378

At here, you use IB UIView ( have class & 1 xib View to custom )? So IB View is unnecessary, xib is view to display.

At UITableView -> add :

[cell getCustomView];

Add methods to get Customview :

// CustomCell.h
-(void) getCustomView;
// CustomCell.h
-(void) getCustomView{
    [customView removeFromSuperview];
    customView = [[CustomView alloc] initWithFrame:customView.frame];
    [self addSubview:customView];
}

Add load xib CustomView:

// CustomView.m
-(id) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
        CustomView *v = (CustomView *)[nibs objectAtIndex:0];
        return v;
    }
    return self;
}

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385510

You need to make a subclass of UITableViewCell. In your subclass's initWithCoder: method, after you've called [super initWithCoder:aDecoder], you can load the other nib and add its view as a subview of the cell (self).

Upvotes: 0

Related Questions