Я хочу установить высоту ячейки динамически на основе высоты метки.Изменение высоты пользовательской ячейки динамически не работает
Я создал ячейку:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"ORDStepsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *stepsLabel;
UILabel *stepsNumberLabel;
UIImageView *bulletImageView;
UIImageView *cellBackgroungImage;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
CGRect stepsLabelFrame = CGRectMake(20, 0, 253, 21);
CGRect bulletImageViewFrame = CGRectMake(281, 25, 29, 29);;
CGRect stepsNumberLabelFrame = CGRectMake(3, 3, 21, 21);
CGRect cellBackgroungImageFrame = CGRectMake(0, 0, 320, 80);
cellBackgroungImage = [[UIImageView alloc] initWithFrame:cellBackgroungImageFrame];
cellBackgroungImage.image = [UIImage imageNamed:@"list-item"];
stepsLabel = [[UILabel alloc] initWithFrame:stepsLabelFrame];
stepsLabel.textAlignment = UITextAlignmentRight;
stepsLabel.tag = 0;
bulletImageView = [[UIImageView alloc] initWithFrame:bulletImageViewFrame];
bulletImageView.image = [UIImage imageNamed:@"step-bullet"];
stepsNumberLabel = [[UILabel alloc] initWithFrame:stepsNumberLabelFrame];
stepsNumberLabel.textAlignment = UITextAlignmentCenter;
stepsNumberLabel.textColor = [UIColor whiteColor];
stepsNumberLabel.backgroundColor = [UIColor clearColor];
stepsNumberLabel.font = [UIFont boldSystemFontOfSize:17.0];
stepsNumberLabel.tag = 1;
[cell.contentView addSubview:cellBackgroungImage];
[cell.contentView addSubview:stepsLabel];
[cell.contentView addSubview:bulletImageView];
[bulletImageView addSubview:stepsNumberLabel];
} else {
stepsLabel = (UILabel *)[cell viewWithTag:0];
stepsNumberLabel = (UILabel *)[cell viewWithTag:1];
}
stepsLabel.text = [preperationSteps objectAtIndex:indexPath.row];
[stepsLabel setBackgroundColor:[UIColor redColor]];
[stepsLabel setNumberOfLines:0];
[stepsLabel sizeToFit];
NSLog(@"cellHeight = %f", cell.frame.size.height);
stepsLabel.frame = CGRectMake(20, (cell.frame.size.height - stepsLabel.frame.size.height)/2, 253, stepsLabel.frame.size.height);
stepsNumberLabel.text = [NSString stringWithFormat:@"%d", indexPath.row + 1];
[cell setSelected:NO];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
return cell;
}
Затем я вычислил высоту лейбла:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 253, 30)];
NSString *labelText = [preperationSteps objectAtIndex:indexPath.row];
cellLabel.text = labelText;
[cellLabel setNumberOfLines:0];
[cellLabel sizeToFit];
cellLabel.frame = CGRectMake(0, 0, 253, cellLabel.frame.size.height);
CGFloat cellLabelHeight = cellLabel.frame.size.height;
NSLog(@"cellLabelHeight = %f", cellLabelHeight + 20);
return cellLabelHeight + 20;
}
В журнале:
2013-03-08 17:37:18.161 Recipe[37547:c07] cellLabelHeight = 125.000000
2013-03-08 17:37:18.161 Recipe[37547:c07] cellLabelHeight = 62.000000
2013-03-08 17:37:18.161 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.162 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.162 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.162 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.163 Recipe[37547:c07] cellLabelHeight = 62.000000
2013-03-08 17:39:34.852 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.852 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.853 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.853 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.854 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.854 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.855 Recipe[37581:c07] cellHeight = 44.000000
Как вы можете видеть высоту ячейки фиксирована 44, хотя значение, возвращаемое с heightForRowAtIndexPath, отличается. Я знаю, что heightForRowAtIndexPath вызывается до cellForRowAtIndexPath (как видно из журнала), так как я могу изменить высоту?
Высота ячейки не установлена в вызове 'cellForRowAtIndexPath'. Этот метод просто возвращает ячейку со стандартной высотой. Когда таблица получит ячейку и добавит ее в таблицу, высота ячейки будет установлена. Таким образом, регистрация высоты в 'cellForRowAtIndexPath' вводит в заблуждение. Просто убедитесь, что пользовательский контент вашей ячейки правильно настраивается, когда высота ячейки в конечном итоге установлена на полную высоту. – rmaddy
Где мне нужно настроить подпункты ячейки, если высота в cellForRowAtIndexPath является стандартной? В willDisplayCell? – oridahan
Это не что иное, как обеспечение того, чтобы subviews правильно сортировали себя с любым другим видом. Используйте правильные 'autoresizingMasks', используйте ограничения или задайте собственный класс ячеек и используйте' layoutSubviews'. – rmaddy