Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableView中动态更改单元格的高度?_Ios_Swift_Uitableview - Fatal编程技术网

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

Ios 如何在UITableView中动态更改单元格的高度?,ios,swift,uitableview,Ios,Swift,Uitableview,我有: myUIViewcontroller中的UITableView var搜索结果:UITableView=UITableView() 自定义UITableViewCell类: class CellSearchResult: UITableViewCell { ... } 我为tableview注册手机: searchResults.registerClass(CellSearchResult.self, forCellReuseIdentifier: "Cell"); 我有我的tabl

我有:

my
UIViewcontroller中的
UITableView

var搜索结果:UITableView=UITableView()

自定义
UITableViewCell
类:

class CellSearchResult: UITableViewCell { ... }
我为tableview注册手机:

searchResults.registerClass(CellSearchResult.self, forCellReuseIdentifier: "Cell");
我有我的tableView方法来填充我的表格:

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

    ...
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
var cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!CellSearchResult

cell.frame.height=100;您需要重写
heightforrowatinexpath
函数。示例:

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

如果您不是子类化
UITableViewController
,则除非符合
UITableViewDelegate
UITableViewDataSource
协议,否则无法访问tableView函数。您可以通过

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


您需要重写
heightforrowatinexpath
函数。示例:

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

如果您不是子类化
UITableViewController
,则除非符合
UITableViewDelegate
UITableViewDataSource
协议,否则无法访问tableView函数。您可以通过

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


如果所有单元格的高度相同,最有效的方法是设置
UITableView
本身的属性

tableView.rowHeight = 100.0
使用时会影响性能 tableView:heightForRowAtIndexPath:而不是rowHeight。每次 显示表格视图时,它将调用表格视图:heightForRowAtIndexPath: 在委托的每一行上,这可能会导致 表视图具有大量数据时存在严重的性能问题 行数(大约1000或更多)

如果单元格的高度可变,则需要实现
UITableViewDelegate
方法
tableView:heightforrow索引路径:

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

    var height = // do something to calculate height

    return height
}
如果您需要在
tableView:heightForRowAtIndexPath:
中执行昂贵的计算,还建议实施另一种
UITableViewDelegate
方法(从iOS 7开始提供)。您在该方法中输入的任何代码都需要高效

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 100.0
}
提供行高度的估计值可以提高用户的使用效率 加载表视图时的经验。如果表包含变量 高度行,计算它们的所有高度和 因此,导致更长的加载时间。使用估算允许您延迟 从加载时间到滚动,几何体计算的一些成本 时间


如果所有单元格的高度相同,最有效的方法是设置
UITableView
本身的属性

tableView.rowHeight = 100.0
使用时会影响性能 tableView:heightForRowAtIndexPath:而不是rowHeight。每次 显示表格视图时,它将调用表格视图:heightForRowAtIndexPath: 在委托的每一行上,这可能会导致 表视图具有大量数据时存在严重的性能问题 行数(大约1000或更多)

如果单元格的高度可变,则需要实现
UITableViewDelegate
方法
tableView:heightforrow索引路径:

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

    var height = // do something to calculate height

    return height
}
如果您需要在
tableView:heightForRowAtIndexPath:
中执行昂贵的计算,还建议实施另一种
UITableViewDelegate
方法(从iOS 7开始提供)。您在该方法中输入的任何代码都需要高效

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 100.0
}
提供行高度的估计值可以提高用户的使用效率 加载表视图时的经验。如果表包含变量 高度行,计算它们的所有高度和 因此,导致更长的加载时间。使用估算允许您延迟 从加载时间到滚动,几何体计算的一些成本 时间


要在运行时增加特定的行高度,而不调用
reloadData()

增加第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
}

要在运行时增加特定的行高度,而不调用
reloadData()

增加第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
}

您可以使用TableView的rowheight属性您可以使用TableView的rowheight属性非常感谢您的帮助。我想这是正确的方法,但我有一些实际问题。将代码粘贴到UIViewcontroller中时,会显示“Methode不会覆盖任何方法…”。删除覆盖时,不会调用该方法。是否必须添加任何协议?我必须输入,即我的“func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell”也没有覆盖。我的UIViewcontroller中有protocoll UITableViewDelegate,但没有调用我的HeightForRowatineXpath方法。非常感谢,它可以工作。我无法复制我所做的,但现在它可以工作了!!太好了!你需要设置UITableViewDelegate和UITableViewDataSource。可能是这导致了问题。你很高兴感谢为了帮助。我想这是正确的方法,但我有一些实际问题。当将代码粘贴到我的UIViewcontroller中时,它会显示“Methode不覆盖任何方法…”。当删除覆盖时,不会调用该方法。我必须添加任何协议吗?我必须添加,即我的“func tableView”(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell“也没有覆盖。我的UIViewcontroller中有protocoll UITableViewDelegate,但没有调用我的HeightForRowatineXpath方法。非常感谢,它可以工作。我无法复制我所做的,但现在它可以工作了!!太好了!你需要设置UITableViewDelegate和UITableViewDataSource。可能是这导致了问题。你很高兴感谢感谢你的帮助和良好的解释,但阿尔达·凯斯金纳是第一位。投票结果是一致的。非常感谢你的帮助和良好的解释,但是阿尔达·凯斯克