从另一个 .m 文件获取项目的高度
Get height of item from another .m file
我在 UITableView 中有一个自定义单元格。我以编程方式创建了一个高度为 200、宽度为 50 的标签。当我在标签宽度和高度的 customCell.m
中执行 NSLog
时,它会给出 w: 50 和 h: 200。但是当我在 mainViewController.m
中执行 NSLog
时,它的高度和宽度为 0。
不确定为什么会这样。我需要在 mainViewController.m
中获取标签的真实高度
这是我的代码:
customCell.m
- (void)awakeFromNib {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:@"Hello"];
[self.myView addSubview:self.label];
CGRect myFrame = self.myView.frame;
myFrame.size.height = self.label.frame.size.height;
self.myView.frame = myFrame;
NSLog(@"%f", self.label.frame.size.height); // Results: 200.0000
}
mainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
customCell *cellVC = [[cutsomCell alloc] init];
NSLog(@"%f, %f", cellVC.label.frame.size.height, cellVC.frame.size.height); // Results: 0.0000, 44.0000
}
我在 StoryBoard 中设置myView
为 0。
有什么不明白的地方欢迎提问,我很乐意为您解答。
更新
这是我的代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:@"Hello"];
[self.myView addSubview:self.label];
view
}
return self;
}
viewDidLoad
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"cellID"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCell = @"customCell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:customCell owner:nil options:nil] objectAtIndex:0];
}
return cell;
}
现在的问题是,当我 运行 应用程序时,没有显示任何单元格。但我确实得到了正确的 NSLog。
在您的 mainViewController 中,您只是使用 alloc 然后使用 init 以编程方式创建了 cellVC,您没有从 nib 创建它,就像您在 customCell.m 中那样,因此没有调用 awakeFromNib 方法。
更新
改回使用 xib 中的自定义单元格(而不是 registerCell)
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCellxib"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCellReuseID"]; // in the viewDidLoad of the mainViewController
然后将单元格出队 - 你真的不需要检查单元格是否为 nil,因为现在保证出队 return 一个单元格
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCellReuseID" forIndexPath:indexPath];
在您的table查看数据源方法中
将代码保留在您的 awakeFromNib 中以初始化从 nib 添加的自定义单元格。 xib 中的默认单元格已在此处完全初始化,因此您应该能够自定义自定义视图。您不再需要 initWithIdentifer
您可以选择实现 table 视图委托方法,例如:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// calculate the row height or return a constant
return 200.0;
}
问题是 -awakeFromNib 没有在 -init 之后直接被调用
试试这个:
- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:@"Hello"];
[self.myView addSubview:self.label];
CGRect myFrame = self.myView.frame;
myFrame.size.height = self.label.frame.size.height;
self.myView.frame = myFrame;
NSLog(@"%f", self.label.frame.size.height); // Results: 200.0000
return self;
}
我在 UITableView 中有一个自定义单元格。我以编程方式创建了一个高度为 200、宽度为 50 的标签。当我在标签宽度和高度的 customCell.m
中执行 NSLog
时,它会给出 w: 50 和 h: 200。但是当我在 mainViewController.m
中执行 NSLog
时,它的高度和宽度为 0。
不确定为什么会这样。我需要在 mainViewController.m
这是我的代码:
customCell.m
- (void)awakeFromNib {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:@"Hello"];
[self.myView addSubview:self.label];
CGRect myFrame = self.myView.frame;
myFrame.size.height = self.label.frame.size.height;
self.myView.frame = myFrame;
NSLog(@"%f", self.label.frame.size.height); // Results: 200.0000
}
mainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
customCell *cellVC = [[cutsomCell alloc] init];
NSLog(@"%f, %f", cellVC.label.frame.size.height, cellVC.frame.size.height); // Results: 0.0000, 44.0000
}
我在 StoryBoard 中设置myView
为 0。
有什么不明白的地方欢迎提问,我很乐意为您解答。
更新
这是我的代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:@"Hello"];
[self.myView addSubview:self.label];
view
}
return self;
}
viewDidLoad
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"cellID"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCell = @"customCell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:customCell owner:nil options:nil] objectAtIndex:0];
}
return cell;
}
现在的问题是,当我 运行 应用程序时,没有显示任何单元格。但我确实得到了正确的 NSLog。
在您的 mainViewController 中,您只是使用 alloc 然后使用 init 以编程方式创建了 cellVC,您没有从 nib 创建它,就像您在 customCell.m 中那样,因此没有调用 awakeFromNib 方法。
更新 改回使用 xib 中的自定义单元格(而不是 registerCell)
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCellxib"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCellReuseID"]; // in the viewDidLoad of the mainViewController
然后将单元格出队 - 你真的不需要检查单元格是否为 nil,因为现在保证出队 return 一个单元格
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCellReuseID" forIndexPath:indexPath];
在您的table查看数据源方法中
将代码保留在您的 awakeFromNib 中以初始化从 nib 添加的自定义单元格。 xib 中的默认单元格已在此处完全初始化,因此您应该能够自定义自定义视图。您不再需要 initWithIdentifer
您可以选择实现 table 视图委托方法,例如:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// calculate the row height or return a constant
return 200.0;
}
问题是 -awakeFromNib 没有在 -init 之后直接被调用
试试这个:
- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:@"Hello"];
[self.myView addSubview:self.label];
CGRect myFrame = self.myView.frame;
myFrame.size.height = self.label.frame.size.height;
self.myView.frame = myFrame;
NSLog(@"%f", self.label.frame.size.height); // Results: 200.0000
return self;
}