我想要有动态的细胞高度。我以编程方式创建了一个自定义单元格。现在,我希望单元格在高度上发生变化,但它似乎总是返回44,这是默认的tableviewcell高度。为什么会这样呢?
- (void)viewDidLoad {
[super viewDidLoad];
[self.reminderTableView registerClass:[ReminderTableViewCell class] forCellReuseIdentifier:@"Daybreak-Cell"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
if (indexPath.row > 3) {
return 60;
}
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"Custom-Cell" forIndexPath:indexPath];
...
return cell;
}自定义单元
@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(@"HEIGHT: %f",self.frame.size.height);
// Initialization code
[self.contentView setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, kDefaultTableViewCellHeight)];
[self setBackgroundColor:[UIColor clearColor]];
[self createViews];
}
return self;
}发布于 2015-08-28 20:49:16
你所需要做的,从iOS7开始,我相信,就是:
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;要使单元格具有动态高度,也不要忘记将约束从上到下放置在单元格内。
发布于 2015-08-28 21:17:53
只需确保将表的委托设置为包含
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
if (indexPath.row > 3) {
return 60;
}
return 70;
}因为这个方法适用于我
https://stackoverflow.com/questions/32279435
复制相似问题