简体   繁体   English

如何动态更改UITableView中单元格的高度?

[英]How to dynamically change the height of a cell in a UITableView?

I have: 我有:

A UITableView in my UIViewcontroller 我的UIViewcontrollerUITableView

var searchResults: UITableView = UITableView();

A custom UITableViewCell class: 一个自定义的UITableViewCell类:

class CellSearchResult: UITableViewCell { ... }

I register my cell for the tableview: 我为表格视图注册单元格:

searchResults.registerClass(CellSearchResult.self, forCellReuseIdentifier: "Cell");

I have my tableView method to fill my table: 我有我的tableView方法来填充我的表:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellSearchResult
    cell.frame.height = 100;   <<<< ERROR

    ...
}

I would like to change the height of my cell like: 我想像这样更改单元格的高度:

cell.frame.height = 100;   <<<< ERROR

How to do this ? 这个怎么做 ?

You need to override heightForRowAtIndexPath function. 您需要重写heightForRowAtIndexPath函数。 Example: 例:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100
    }

EDIT: 编辑:

If you are not subclassing UITableViewController , you can't access tableView functions unless you conform UITableViewDelegate , UITableViewDataSource protocols. 如果您不是UITableViewController子类,则除非符合UITableViewDelegateUITableViewDataSource协议,否则无法访问tableView函数。 You can do this by 你可以这样做

class myViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { ....

.

  tableview.delegate = self
  tableview.datasource = self

If the heights of all cells are the same the most efficient way is to set the rowHeight property of the UITableView itself. 如果所有单元格的高度都相同,则最有效的方法是设置UITableView本身的rowHeight属性。

tableView.rowHeight = 100.0

There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. 使用tableView:heightForRowAtIndexPath:而不是rowHeight会对性能产生影响。 Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: 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). 每次显示表视图时,它都会在委托中为其表的每一行调用tableView:heightForRowAtIndexPath:,这可能导致表视图具有大量行(大约1000或更多)而导致严重的性能问题。

If the cells have variable heights you will need to implement the UITableViewDelegate method tableView:heightForRowAtIndexPath: 如果单元格的高度可变,则需要实现UITableViewDelegate方法tableView:heightForRowAtIndexPath:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    var height = // do something to calculate height

    return height
}

If you need to do expensive calculations in tableView:heightForRowAtIndexPath: it is also advisable to implement another UITableViewDelegate method, tableView:estimatedHeightForRowAtIndexPath: (available since iOS 7). 如果您需要在tableView:heightForRowAtIndexPath:进行昂贵的计算tableView:heightForRowAtIndexPath:建议您另外实现另一个UITableViewDelegate方法tableView:estimatedHeightForRowAtIndexPath:自iOS 7起可用)。 Any code you put in this method needs to be efficient. 您放入此方法的任何代码都必须高效。

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 100.0
}

Providing an estimate the height of rows can improve the user experience when loading the table view. 提供估计行的高度可以改善加载表视图时的用户体验。 If the table contains variable height rows, it might be expensive to calculate all their heights and so lead to a longer load time. 如果表包含可变高度的行,则计算它们的所有高度可能会很昂贵,因此会导致更长的加载时间。 Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time. 使用估计可以使几何计算的某些成本从加载时间推迟到滚动时间。

You have to implement the following to increase a particular row height at run time without calling the reloadData() 您必须实现以下内容以在运行时增加特定的行高,而无需调用reloadData()

Example to increase height of row 10: 增加第10行高度的示例:

 rowHightUpdateReqd = true
 tableView.beginUpdates()
 tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 10, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade)
 tableView.endUpdates()

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

   var rowHight: CGFloat = 50

   if rowHightUpdateReqd == true{
      rowHight = 100
      rowHightUpdateReqd = false
   }
   return rowHight
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
 return 100.0;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UITableView静态单元格中动态更改单元格高度 - How to change cell height dynamically in UITableView static cell 如何在Swift中动态更改包含UICollectionView单元格的UITableView单元格的高度? - How to dynamically change the height of a UITableView Cell containing a UICollectionView cell in Swift? UITableView:如何在单击按钮时动态更改单元格高度? - UITableView: How to change cell height dynamically when a button is clicked in it? UITableView:如何在单击按钮时动态更改单元格高度? 迅速 - UITableView: How to change cell height dynamically when a button is clicked in it? Swift 如何动态调整UITableView单元格的高度 - How to dynamically adjust the height of a UITableView cell 动态更改UITableView高度 - Change UITableView height dynamically 如何在Swift中更改UITableView单元格的高度? - How to change height of UITableView cell in Swift? 如何在UITableView和Pixate中更改单元格高度 - How to change cell height in UITableView and Pixate UITableview:单击自定义按钮时动态更改单元格高度 - UITableview : Change cell height dynamically when clicked the custom button 如何使UITableView单元在用户键入时动态更改其高度? - How to make a UITableView cell dynamically change its height while user is typing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM