ios - 基类的 loadNibNamed 覆盖子类的实例

标签 ios objective-c iphone loadnibnamed initwithstyle

我有类层次结构

ParentCell extends UITableViewCell
ChildCell extends ParentCell

ParentCell 有单独的 XIB,在子单元中,我仅创建一个按钮并将其添加到 ParentCell XIB 的一个 View 中。但我无法为此按钮添加操作。因为即使我为 ChildCell 创建一个实例,它也会返回 ParentCell 的实例

因为我使用 loadNibNamed 来获取带有 IBOutlet 连接的 XIB。 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;
}

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
            }
}

现在通过这种方式暂时解决

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;
}

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;
}

但我不知道这是正确的方法,或者我们还有其他更好的方法..

最佳答案

您应该在 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];

(不要忘记在 XIB 文件中设置适当的 ReuseIdentifier)

这是最佳实践,您可以摆脱 initWithStyle 实现。

关于ios - 基类的 loadNibNamed 覆盖子类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637577/

相关文章:

ios - iOS:如何获取Youtube视频流网址?

iphone - 将图 block 坐标转换为像素再转换为地理坐标

iphone - 如何发送带有压缩内容的 HTTP POST 请求?

ios - Core Data一对多关系保存

ios - 将十六进制字符串转换为base64?

ios - 应用程序从后台终止后是否可以继续下载文件

objective-c - iOS 设备上的钥匙串(keychain)可以保存与 NSString 不同的东西吗?

iphone - 仅显示几张图像,滚动不顺畅

iphone - 在 iPhone 中保存 session 数据

ios - NSCoder对c数据类型的编码和解码