Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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_Xcode_Uitableview - Fatal编程技术网

Ios 表格视图显示正确的单元格´;滚动一段时间后的高度是多少

Ios 表格视图显示正确的单元格´;滚动一段时间后的高度是多少,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我对表格vie单元格有一些问题,只是在滚动了一些单元格之后,单元格才获得了正确的高度。 . 添加了layoutIfNeeded(),但我似乎不工作 我的代码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView === MainTable{ if let cell = tableVi

我对表格vie单元格有一些问题,只是在滚动了一些单元格之后,单元格才获得了正确的高度。 . 添加了layoutIfNeeded(),但我似乎不工作

我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView === MainTable{
            if let cell = tableView.dequeueReusableCell(withIdentifier: "MedName", for: indexPath) as? MedCell{
                
                let backgroundView              =  UIView()
                backgroundView.backgroundColor  =  UIColor.white.withAlphaComponent(0.0)
                cell.selectedBackgroundView     =  backgroundView
                                
                cell.setMedName(name: self.medCatalog[indexPath.row].nombre, uso: self.medCatalog[indexPath.row].uso )
               
                cell.layoutIfNeeded()
                cell.updateConstraintsIfNeeded()
                cell.layoutSubviews()

                
               
                self.cellBool[indexPath.row] = true
                self.collapsedCellHeight[indexPath.row] = cell.medText.bounds.height

                return cell
            }
        }
        self.cellBool[indexPath.row]            =  false
        self.collapsedCellHeight[indexPath.row] =  0.0
        self.expandedCellHeight[indexPath.row]  =  0.0
        return UITableViewCell()
    }


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if self.cellBool[indexPath.row] != nil {
            if self.cellBool[indexPath.row]!{
                
                let nameHeight             =  self.collapsedCellHeight[indexPath.row]!
                let topAnchor: CGFloat     =  12.0
                let bottomAnchor: CGFloat  =  10.0

                let cellHeight             =  nameHeight + topAnchor + bottomAnchor + 9 + 2

                return cellHeight

            }
        }
        return 85
    }
编辑 我忘了提到主要目标是创建一个可扩展和可折叠的行,这就是为什么在蓝色UIlabel下面有一个UIlabel。
蓝色uilabel文本来自json。json中的一些值有点长,因此uilabel会创建另一行来显示所有内容。这就是我需要提供单元格高度的原因,因为有些标签有一行,而其他标签有多行,我需要计算蓝色标签的高度,然后在
didSelectRowAtIndexPath
上展开整个单元格。我知道如何展开和折叠数据行,但我不知道为什么表的行为如此

我通过替换

cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()


您可以更好地使用autolayout自动计算单元格高度,并删除此方法
func tableView(u tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
。很抱歉不够清楚,我更新了帖子。
cell.layoutIfNeeded()
cell.layoutSubviews()
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()