I have UITableView is Storyboard. I try to create UITableViewCell in Xib and register it in UITableView as that:
-(void)viewDidLoad{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"FlipperTableViewCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"fliperCell"];
}
It work perfectly.
But this cell must have one view inside and flip it to another view when button on first view pressed.
I create another two views in same xib. File's owner is FliperTableViewCell class. Class of first view is FliperTableViewCell.
But now a can't figure out how to add second view (UIView) as subview in first view (UITableViewCell), which loaded by table view. I try to get second view from loadNibNamed:, but it became infinite cycle.
@implementation FlipperTableViewCell
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]){
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil] objectAtIndex:1]];
}
return self;
}
What is another way to get second view from same xib and add it as subview when first view loaded?
problem was solved when I replace
[self.tableview registerNib:nib forCellReuseIdentifier:@"FlipperTableViewCell"];
to
[self.tableview registerClass:[FlipperTableViewCell class] forCellReuseIdentifier:@"FlipperTableViewCell"];
then remove first view from xib and rewrite FlipperTableViewCell to initiate in code
#import "FlipperTableViewCell.h"
@interface TableViewCell ()
@property (nonatomic, strong) UIView *firstView;
@property (nonatomic, strong) UIView *secondView;
@end
@implementation FlipperTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
self.firstView = nibObjects[0];
self.secondView = nibObjects[1];
[self.contentView addSubview:self.firstView];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
// this is for test changing views
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (self.contentView.subviews[0] == self.firstView){
[self.firstView removeFromSuperview];
[self.contentView addSubview:self.secondView];
}
else{
[self.secondView removeFromSuperview];
[self.contentView addSubview:self.firstView];
}
}
@end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.