简体   繁体   English

基础类的loadNibNamed覆盖子类的实例

[英]loadNibNamed at Base Class override the instance of Child Class

I have class hierachy with 我有班级

ParentCell extends UITableViewCell
ChildCell extends ParentCell

ParentCell have separate XIB, In child cell i was creating and adding only one button to one view at ParentCell XIB. ParentCell有单独的XIB,在子单元格中,我正在创建单个父按钮并将其添加到ParentCell XIB的一个视图中。 but i cant add action for this button. 但我不能为此按钮添加操作。 Because even i was creating a instance for ChildCell, that returns the instance of ParentCell 因为即使我正在为ChildCell创建实例,该实例也返回ParentCell的实例

Because i use loadNibNamed to get the XIB with IBOutlet connections. 因为我使用loadNibNamed来获取具有IBOutlet连接的XIB。 @ initWithStyle method in ParentCell Class ParentCell类中的@ initWithStyle方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self = [[NSBundle mainBundle] loadNibNamed:@"ParentCell" owner:self options:nil]
               [0];
    }
    return self;
}

@ initWithStyle method in ChildCell Class @ChildCell类中的initWithStyle方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.button=[[UIButton alloc] init];
        [self.contentView addSubview:button];
    }
    return self;
}

@ View Controller @视图控制器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            static NSString *CellIdentifier = @"ChildCell ";
            ChildCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil)
            {
              cell=[[ChildCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];

                NSLog(@"Cell : %@", cell);  //this have instance of ParentCell instead of ChildCell
            }
}

Now temporarily solved by this way 现在暂时用这种方式解决

@ initWithStyle method in ParentCell Class ParentCell类中的@ initWithStyle方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *views = [mainBundle loadNibNamed:@"ParentCell"
                                            owner:self
                                          options:nil];
       //Here we are linking the view with appropriate IBOutlet by their tag
       self.lblTitle=[views[0] viewWithTag:100];
       self.lblContent=[views[0] viewWithTag:200];
    }
    return self;
}

@ initWithStyle method in ChildCell Class @ChildCell类中的initWithStyle方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.button=[[UIButton alloc] init];
        [self.contentView addSubview:button];
    }
    return self;
}

But i don't know this is right approach to follow, or we have some other better approach then this.. 但是我不知道这是正确的方法,否则我们还有其他更好的方法。

You should use the registerNib:forCellReuseIdentifier: method at the viewDidLoad of you UITableView class. 您应该在UITableView类的viewDidLoad处使用registerNib:forCellReuseIdentifier:方法。

static NSString *parentCellIdentifier = @"parentCellIdentifier";
static NSString *childCellIdentifier = @"childCellIdentifier";

[self.tableView registerNib:[UINib nibWithNibName:@"ParentCell" bundle:nil] forCellReuseIdentifier:parentCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:@"ChildCell" bundle:nil] forCellReuseIdentifier:childCellIdentifier];

(Don't forget to set the appropriate ReuseIdentifier at the XIB file) (不要忘记在XIB文件中设置适当的ReuseIdentifier)

This is the best practice, and you can get rid of your initWithStyle implementations. 这是最佳做法,您可以摆脱initWithStyle实现。

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

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