Swift:UITableViewCell 行高 [英] Swift: UITableViewCell rowheight

查看:31
本文介绍了Swift:UITableViewCell 行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问的问题已经问了很多,但并不完全是我想要的.

The question I'm going to ask is already asked a lot but not exactly how I want it.

这很简单,我想更改特定单元格的行高,我想在 UITableViewCell 类本身中执行此操作.我试图从我的 UIViewController 调用 tableview:

It's very simple, I want to change the row height of a specific cell and I want to do this in the UITableViewCell Class itself. I tried to call the tableview from my UIViewController:

ViewController().tableView.rowHeight = 100

但后来我得到一个错误.所以我的问题是,是否可以更改 tableviewcell 类本身的行高?如果是这样,如何?

But then I get an error. So my question is, is it possible to change the rowheight in the tableviewcell class itself? If so, how?

谢谢!

推荐答案

UPDATE: Response to 评论

从一个标志开始,以跟踪单元格是否应该展开:

UPDATE: Response to Comment

Start with a flag to keep track of whether the cell should expand or not:

let cellShouldExpand = false

当你想要动画高度的变化时,调用以下方法:

When you want to animate the change in height call the following methods:

cellShouldExpand = true
tableView.beginUpdates()
tableView.endUpdates()

并将heightForRowAt:改为如下:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath == indexPathOfCellYouWantToChange && cellShouldExpand {
    return 100 // the height you want
  } else {
    return UITableViewAutomaticDimension
  }
}

执行相反的操作以恢复更改(即设置 cellShouldExpand = false 并再次调用 tableview 更新函数).

Do the opposite to revert the change (i.e. set cellShouldExpand = false and call the tableview update functions again).

如果您希望所有单元格具有相同的高度,那么您可以更改 rowHeight,但正确的方法不是执行 ViewController()... 因为它实例化了一个新的视图控制器.相反,您可以像这样简单地自动引用 tableview:

If you want all cells to have the same height then you can change the rowHeight, except the correct way to do so is not by doing ViewController()... as that instantiates a new view controller. Instead you can simply refer to the tableview automatically like so:

tableView.rowHeight = 100

self.tableView.rowHeight = 100

如果您想为特定单元格设置不同的行高,您需要使用 UITableViewDelegate.在文件顶部声明视图控制器时添加委托一致性:

If you want to have a different row height for a specific cell you need to use UITableViewDelegate. You add the delegate conformance when you declare the view controller at the top of the file:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

然后在 ViewController 类中,您可以实现以下方法:

and then inside the ViewController class you can implement the following method:

let indexPathOfCellYouWantToChange = IndexPath(row: 2, section: 0)

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath == indexPathOfCellYouWantToChange {
    return 100 // the height you want
  } else {
    return UITableViewAutomaticDimension
  }
}

(indexPathOfCellYouWantToChange 应设置为您有兴趣更改高度的单元格的行和部分)

(indexPathOfCellYouWantToChange should be set to the row and section of the cell you're interested in changing the height of)

Apple 文档提供了有关如何正确使用此方法的更多信息:https://developer.apple.com/reference/uikit/uitableviewdelegate/1614998-tableview

The Apple documentation has more info on how to properly use this method: https://developer.apple.com/reference/uikit/uitableviewdelegate/1614998-tableview

该方法允许委托指定具有不同高度的行.如果实现了此方法,它返回的值将覆盖为给定行的 UITableViewrowHeight 属性指定的值.使用 tableView(_:heightForRowAt:) 而不是 rowHeight 属性会影响性能.每次显示 table view 时,它都会在其每一行的委托上调用 tableView(_:heightForRowAt:),这可能会导致具有大量行的 table view 的严重性能问题(大约 1000 或更多).另见tableView(_:estimatedHeightForRowAt:).

Discussion

The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row. There are performance implications to using tableView(_:heightForRowAt:) instead of the rowHeight property. Every time a table view is displayed, it calls tableView(_:heightForRowAt:) on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more). See also tableView(_:estimatedHeightForRowAt:).

这篇关于Swift:UITableViewCell 行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆