2

我有一个表视图,其中包含一组静态的 2 行(在情节提要中创建)。我有一个特定的 UITableViewCell ,其中包含一个动态生成大小的文本框。我还想动态设置包含 UITableViewCell 的高度,但是为 UITableViewCell 设置 .frame 似乎不起作用。有没有人在不使用 tableView:heightForRowAtIndexPath: 的情况下以编程方式完成此操作?

当前代码:

CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [self.place.details sizeWithFont:self.descriptionLabel.font 
                                  constrainedToSize:maximumLabelSize 
                                      lineBreakMode:self.descriptionLabel.lineBreakMode]; 

CGRect labelFrame = self.descriptionLabel.frame;
labelFrame.size.height = expectedLabelSize.height;
// this does not change the height of the UITableViewCell....
self.descriptionTableViewCell.frame = labelFrame;
4

1 回答 1

2

As you've noticed regardless of the height of the UITableViewCell, it is clipped to the height of the UITableView's row for that index path. By the time tableView:cellForRowAtIndexPath: is called the row heights have already been laid out. Just use tableView:heightForRowAtIndexPath: as this is what it is intended for. A key point is that the table view needs to know the height of all its rows to set up its scroll view, scroll bars, etc. On the other hand it only needs to have the actual cells for what is on screen. See this answer for more discussion on why the UITableViewDelegate works this way.

于 2012-08-16T04:26:50.060 回答