OGeek|极客世界-中国程序员成长平台

标题: ios - 来自 NIB 的 UITableViewCell 中的子类 UIView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:50
标题: ios - 来自 NIB 的 UITableViewCell 中的子类 UIView

我在 IB 中创建了一个包含自定义 UIView 的 UITableViewCell。自定义 UIView 也包含在 NIB 中。如何将此自定义 UIView 加载到自定义 UITableViewCell 中?



Best Answer-推荐答案


在这里,您使用 IB UIView(自定义类和 1 个 xib View )?所以 IB View 是不必要的,xib 是要显示的 View 。

在 UITableView -> 添加:

[cell getCustomView];

添加获取自定义 View 的方法:

// CustomCell.h
-(void) getCustomView;
// CustomCell.h
-(void) getCustomView{
    [customView removeFromSuperview];
    customView = [[CustomView alloc] initWithFrame:customView.frame];
    [self addSubview:customView];
}

添加加载xib CustomView:

// CustomView.m
-(id) initWithFrameCGRect)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;
}

关于ios - 来自 NIB 的 UITableViewCell 中的子类 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17180434/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4