混淆单元格的高度,UITableViewCell,iOS
confusing about the height of cell, UITableViewCell,iOS
我正在从 xib 加载一个自定义单元格,其中单元格的高度为 64,然后使用 heightForRowAtIndexPath
更改单元格的高度,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
UINib *cellNIB = [UINib nibWithNibName:NSStringFromClass([TableCell class]) bundle:nil];
[tableView registerNib:cellNIB forCellReuseIdentifier:NSLocalizedString(@"TableCell", nil)];
[self.view addSubview:tableView];
}
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 400 ;
}
在cellForRowAtIndex
,我正在打印单元格的高度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSLocalizedString(@"TableCell", nil)];
NSLog(@"height at:%ld is %@",(long)indexPath.item,NSStringFromCGRect(cell.frame));
[cell setGradient];
return cell;
}
有趣的是,控制台给了我 64 的单元格高度(来自 xib)
应该是 400 吗?
为什么我得到 64 而不是 400 的任何想法。欢迎在这里发表任何评论。
谢谢
PS :
@implementation TableCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
其实很简单。
在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
单元格被重用(或者如果重用队列为空则创建)。
单元格初始高度是
- 44pt(对于完全从代码加载的单元格)
- 您在笔尖中指定的高度(在您的情况下为 64pt)
但是,单元格的大小会根据
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
调整为您 return 的值
在你 return 它来自 cellForRowAtIndexPath:
.
之后
所以您看到的行为完全正确。
提示:执行
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
并在此处记录高度,您会看到在您的情况下单元格已调整为 [400]。
我正在从 xib 加载一个自定义单元格,其中单元格的高度为 64,然后使用 heightForRowAtIndexPath
更改单元格的高度,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
UINib *cellNIB = [UINib nibWithNibName:NSStringFromClass([TableCell class]) bundle:nil];
[tableView registerNib:cellNIB forCellReuseIdentifier:NSLocalizedString(@"TableCell", nil)];
[self.view addSubview:tableView];
}
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 400 ;
}
在cellForRowAtIndex
,我正在打印单元格的高度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSLocalizedString(@"TableCell", nil)];
NSLog(@"height at:%ld is %@",(long)indexPath.item,NSStringFromCGRect(cell.frame));
[cell setGradient];
return cell;
}
有趣的是,控制台给了我 64 的单元格高度(来自 xib)
应该是 400 吗?
为什么我得到 64 而不是 400 的任何想法。欢迎在这里发表任何评论。 谢谢
PS :
@implementation TableCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
其实很简单。
在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
单元格被重用(或者如果重用队列为空则创建)。
单元格初始高度是
- 44pt(对于完全从代码加载的单元格)
- 您在笔尖中指定的高度(在您的情况下为 64pt)
但是,单元格的大小会根据 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
调整为您 return 的值
在你 return 它来自 cellForRowAtIndexPath:
.
所以您看到的行为完全正确。
提示:执行- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
并在此处记录高度,您会看到在您的情况下单元格已调整为 [400]。