2

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?

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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).

3 Comments

Inside - (id)initWithCoder:(NSCoder *)aDecoder in the subclass of the UITableViewCell, the app crashes and cites "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CustomTableViewCell 0x1f546ec0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key titleLabel"
titleLabel is a label on the custom UIView that the cell is trying to load
Edit your question to include the full stack trace of the crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.