如何让 UITableView 行高自动调整为 UITableViewCell 的大小?

发布于 2024-10-19 23:45:13 字数 180 浏览 6 评论 0原文

如何让 UITableView 行高自动调整为 UITableViewCell 的大小?

因此,假设我在 Interface Builder 中创建 UITableViewCell,并且它的高度超过标准大小,我怎样才能让 UITableView 行高自动调整大小呢? (即与必须在界面生成器中手动测量高度并以编程方式设置高度相反)

How to get a UITableView row height to auto-size to the size of the UITableViewCell?

So assuming I'm creating the UITableViewCell in Interface Builder, and it height is more than the standard size, how can I get the UITableView row height to autosize to it? (i.e. as opposed to manually having to measure the height in interface builder and than programmatically setting it)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最丧也最甜 2024-10-26 23:45:13

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height / 是一个很棒的教程。

主要位是

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  // Get the text so we can measure it
  NSString *text = [items objectAtIndex:[indexPath row]];
  // Get a CGSize for the width and, effectively, unlimited height
  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
  // Get the size of the text given the CGSize we just made as a constraint
  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  // Get the height of our measurement, with a minimum of 44 (standard cell size)
  CGFloat height = MAX(size.height, 44.0f);
  // return the height, with a bit of extra padding in
  return height + (CELL_CONTENT_MARGIN * 2);
}

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ is a great tutorial for this.

The main bit is

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  // Get the text so we can measure it
  NSString *text = [items objectAtIndex:[indexPath row]];
  // Get a CGSize for the width and, effectively, unlimited height
  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
  // Get the size of the text given the CGSize we just made as a constraint
  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  // Get the height of our measurement, with a minimum of 44 (standard cell size)
  CGFloat height = MAX(size.height, 44.0f);
  // return the height, with a bit of extra padding in
  return height + (CELL_CONTENT_MARGIN * 2);
}
那支青花 2024-10-26 23:45:13

如果所有单元格都相同,请将 UITableView 上的 rowHeight 属性设置为单元格的大小。如果它们根据内容而不同,您将必须实现 -tableView:heightForRowAtIndexPath: 并根据您的数据源计算每行的高度。

If all the cells are the same, set the rowHeight property on UITableView to the size of your cell. If they're all different based on content, you're going to have to implement -tableView:heightForRowAtIndexPath: and calculate the height for each row based on your data source.

等风也等你 2024-10-26 23:45:13

在带有表格视图的 xib 中,您可以添加一个单元格对象并将其链接到源代码中的 IBOutlet。您不会在任何地方使用它,您只会使用它来获取单元格的高度。

然后,在 tableView:heightForRowAtIndexPath: 中,您可以使用该对象来获取高度。它不是 100% 自动的,但至少这可以省去您在 IB 中的单元格视图中进行更改时手动更新源代码的麻烦。


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return myDummyCellObject.bounds.size.height;
}

如果所有行都属于同一类型(单元格),您可以通过编程方式设置 tableView.rowHeight 属性,而不是实现上面的委托方法。取决于你的场景。

哦,请确保不要忘记在 -dealloc 中释放 myDummyCellObject。

In the xib with your tableview, you can add a cell object and link it to an IBOutlet in your source-code. You won't use it anywhere, you will just use this to get the height of the cell.

Then, in tableView:heightForRowAtIndexPath: you can use that object to get the height. It's not 100% automatic, but at least this saves you the trouble to manually update your source code when you make changes in the cell view in IB.


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return myDummyCellObject.bounds.size.height;
}

If all rows are of the same type (cell) you can programatically set the tableView.rowHeight property instead of implementing the delegate method above. Depends on your scenario.

Oh, and make sure you don't forget to release myDummyCellObject in -dealloc.

扶醉桌前 2024-10-26 23:45:13
self.tableView.estimatedRowHeight = 65.0; // Estimate the height you want
self.tableView.rowHeight = UITableViewAutomaticDimension; // auto change heights for multiple lines cells.

实现 UITableViewDataSource 时所需方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [self.todoArray objectAtIndex:indexPath.row];
    cell.textLabel.numberOfLines = 0; // allow multiple lines showing up

    return cell;
}
self.tableView.estimatedRowHeight = 65.0; // Estimate the height you want
self.tableView.rowHeight = UITableViewAutomaticDimension; // auto change heights for multiple lines cells.

When implement UITableViewDataSource required method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [self.todoArray objectAtIndex:indexPath.row];
    cell.textLabel.numberOfLines = 0; // allow multiple lines showing up

    return cell;
}
夜司空 2024-10-26 23:45:13

从 iOS 8 开始,您可以选择工作通过在表格视图上指定这些选项来使用自动调整单元格大小

tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension

只要系统可以根据现有约束或内容计算行,这就会起作用。请注意,如果您设置automaticDimension,则不会调用heightForRowAtIndexPath!


有时,对于更复杂的数据,某些单元格可以自动计算,但其他单元格则需要特定的高度计算逻辑。在这种情况下,您应该设置estimatedRowHeight,然后使用可以正常工作的单元格的自动逻辑实现cellForRowAtIndexPath:

// Set the estimated row height in viewDidLoad, but *not* automatic dimension!
override func viewDidLoad() {
    // other viewDidLoad stuff...
    tableView.estimatedRowHeight = 85.0
    tableView.delegate = self
}

// Then implement heightForRowAtIndexPath delegate method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if theAutomaticCalculationWorksForThisRow {
        return UITableView.automaticDimension
    } else {
        // Calculate row height based on custom logic...
        let rowHeight = 85.0
        return rowHeight
    }
}

As of iOS 8 you have the option to work with self-sizing cells by specifying these options on your table view:

tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension

This will work as long as the system can calculate the rows based on existing constraints or content. Take care that if you set automaticDimension then heightForRowAtIndexPath will not be called!


Sometimes with more complex data some cells can be calculated automatically, but others need a specific height calculation logic. In this case you should only set the estimatedRowHeight and then implement cellForRowAtIndexPath with automatic logic for the cells that can work correctly:

// Set the estimated row height in viewDidLoad, but *not* automatic dimension!
override func viewDidLoad() {
    // other viewDidLoad stuff...
    tableView.estimatedRowHeight = 85.0
    tableView.delegate = self
}

// Then implement heightForRowAtIndexPath delegate method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if theAutomaticCalculationWorksForThisRow {
        return UITableView.automaticDimension
    } else {
        // Calculate row height based on custom logic...
        let rowHeight = 85.0
        return rowHeight
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文