代码之家  ›  专栏  ›  技术社区  ›  user1107173

iOS:带有代码和IB的自定义单元格财产不会同时出现

  •  0
  • user1107173  · 技术社区  · 11 年前

    自定义单元格.h

    @interface CustomCell : UITableViewCell
    //Properties created in Code, not via IB.
    @property (nonatomic, strong) UILabel *labelUsername;
    @property (nonatomic, strong) UIView *circle;
    
    //Properties Created through IB by control+drag
    @property (nonatomic, strong) UILabel *labelFirstName;
    @property (nonatomic, strong) UILabel *labelLastName;
    
    
    @end
    

    自定义单元格.m

    @implementation CustomCell
    -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            //Creating properties in code
            self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
            [self.circle setBackgroundColor:[UIColor brownColor];
    
            self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
            self.labelUsername.textColor = [UIColor blackColor];
    
            [self.contentView addSubview:self.labelUsername];
            [self.contentView addSubview:self.circle];
        }
        return self;
    }
    

    表视图.m

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *customCellIdentifier = @"CustomCell";
    
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
        if (cell == nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inviteCellIdentifier];
        }
    
        cell.labelFirstName.text = @"FirstName";
        cell.labelLastName.text = @"LastName";
        return cell;
    }
    

    上面的代码继续显示 labelUsername circle 。但是,使用IB创建的财产( labelFirstName labelLastName )没有出现。

    所以在tableView.m中 viewDidLoad 我使用以下代码注册了Nib:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
                                                   bundle:[NSBundle mainBundle]]
             forCellReuseIdentifier:@"CustomCell"];
    }
    

    现在 标签名字 标签姓氏 出现,但使用代码创建的财产( label用户名 圆圈 )不要出现。

    我如何才能让所有4个财产都显示出来?

    2 回复  |  直到 11 年前
        1
  •  0
  •   Akhilrajtr    11 年前

    在中尝试 CustomCell.m ,

    - (void)awakeFromNib
    {
        [super awakeFromNib];
        self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
        [self.circle setBackgroundColor:[UIColor brownColor];
    
        self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
        self.labelUsername.textColor = [UIColor blackColor];
    
        [self.contentView addSubview:self.labelUsername];
        [self.contentView addSubview:self.circle];
    }
    
        2
  •  0
  •   Basheer_CAD    11 年前

    请稍后再试 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"yourNibName" owner:self options:NULL];
    [self addSubview:[views lastObject]];