Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 表视图行的高度索引路径不工作_Ios_Swift_Uitableview - Fatal编程技术网

Ios 表视图行的高度索引路径不工作

Ios 表视图行的高度索引路径不工作,ios,swift,uitableview,Ios,Swift,Uitableview,我有表格视图来显示用户的评论 我需要使每一行的高度根据内容高度而动态变化 我找了一下,发现了 行高度索引路径方法 但它不起作用,或者我不知道如何使用它 这是我的密码: func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPa

我有表格视图来显示用户的评论

我需要使每一行的高度根据内容高度而动态变化

我找了一下,发现了

行高度索引路径方法

但它不起作用,或者我不知道如何使用它

这是我的密码:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
   let username = cell.viewWithTag(1) as! UILabel

    let comment = cell.viewWithTag(2) as! UITextView
    username.text = usernames[indexPath.row]

    comment.text = comments[indexPath.row]

    return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.comments.count
}

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

您没有正确实现
heightforrowatinexpath
方法。你应该仔细阅读,因为它会做你想做的事

基本上,要使用自调整大小的单元格,对于
UITableView
,您将设置估计的行高,并将行高设置为
UITableView自动标注
(或Swift 4.2或更高版本中的
UITableView.automaticDimension

在Swift 4.2之前:

tableView.estimatedRowHeight = 85.0
tableview.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension
Swift 4.2:

tableView.estimatedRowHeight = 85.0
tableview.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension
将“估计行高”值设置为与所有单元格的粗略平均高度非常接近的值。这有助于iOS了解完整的
UIScrollView
内容有多大

此外,使用自调整单元格大小时,您根本不会实现
heightforrowatinexpath
。每个单元高度是从每个单元内的约束中获得的

有关自动调整单元格大小的良好指南,请查看


如果不想对单元格进行自调整大小,可以实现
heightforrowatinexpath
,但需要为每个单元格返回正确的高度。该逻辑将由您根据
indepath
参数确定。但是您需要确保返回每个单元格的高度(由
indepath
指定),以像素(逻辑像素)为单位.

也许这篇文章会有所帮助,或者这篇文章我删除了RowIndeXPath的Heights,并使用了self.myTable.estimatedRowHeight=160 self.myTable.rowHeight=UITableViewAutomaticDimensions您需要确保您的单元格都使用自动布局正确设置。如果没有适当调整大小,它将无法工作。您是说约束?是的,需要正确设置自动布局约束,以便自调整大小的单元格知道每个单元格的大小。