gpt4 book ai didi

ios - 以编程方式创建自定义 UITableViewCell - Objective C

转载 作者:行者123 更新时间:2023-11-29 12:13:22 25 4
gpt4 key购买 nike

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
//[self createViews];
}
return self;
}

- (void)drawRect:(CGRect)rect
{
NSLog(@"draw rect");
[self createViews];
}

我正在创建自定义 UITableViewCell。我需要创建一个取决于 UITableViewCell 高度的 UILabel,并且高度尚未在 initWithStyle 中设置(它返回默认值 44,而实际上是我的高度细胞变化很大)。为此,我在 drawRect 中调用了我的 createViews 函数。这运行良好,但我注意到当我插入和删除行时可以再次调用该函数。

我的问题:drawRect 中调用我的 createViews 函数是否有意义?

最佳答案

您在这里几乎没有选择。

1. 使用 layoutSubviews/awakeFromNib,检查是否创建了 subview ,如果没有,则使用正确的框架创建它们。

2. 使用 init 创建 View :

  • 约束条件
  • 在没有约束的情况下,在 layoutSubviews/awakeFromNib 中尝试改变框架

关于ios - 以编程方式创建自定义 UITableViewCell - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com