简体   繁体   English

从同一个xib加载另一个UIView以添加为子视图

[英]load another UIView from same xib to add as subview

I have UITableView is Storyboard. 我有UITableView是Storyboard。 I try to create UITableViewCell in Xib and register it in UITableView as that: 我尝试在Xib中创建UITableViewCell并将其注册在UITableView中,如下所示:

-(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. 我在同一个xib中创建了另外两个视图。 File's owner is FliperTableViewCell class. 文件的所有者是FliperTableViewCell类。 Class of first view is FliperTableViewCell. 第一个视图的类是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. 但是现在还不知道如何在表视图中加载的第二个视图(UIView)作为子视图添加到第一个视图(UITableViewCell)中。 I try to get second view from loadNibNamed:, but it became infinite cycle. 我尝试从loadNibNamed:获取第二个视图,但是它变成了无限循环。

@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? 从相同的xib获取第二个视图并将其作为子视图添加到第一个视图时的另一种方法是什么?

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 然后从xib中删除第一个视图并重写FlipperTableViewCell以在代码中启动

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM