Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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 当我想添加动态文本时,如何自动调整UITableViewCell的大小_Ios_Swift - Fatal编程技术网

Ios 当我想添加动态文本时,如何自动调整UITableViewCell的大小

Ios 当我想添加动态文本时,如何自动调整UITableViewCell的大小,ios,swift,Ios,Swift,我正在用很少的单元格设置UITableView。当我按住单元格时,将显示一个菜单,我可以在单元格中添加注释/文本。 我想要的是,如果用户不插入任何注释,则不显示注释标签的空白。但是,如果用户插入注释,则该标签将显示,并显示插入的整个注释。 是否可以在不硬编码单元格高度的情况下执行此操作?因为此应用程序将在多个设备上运行,并且每个设备的屏幕都不同 我已尝试在viewDidLoad中使用以下代码行使用自动布局,但未成功: questionsTableView.rowHeight = UITableV

我正在用很少的单元格设置UITableView。当我按住单元格时,将显示一个菜单,我可以在单元格中添加注释/文本。 我想要的是,如果用户不插入任何注释,则不显示注释标签的空白。但是,如果用户插入注释,则该标签将显示,并显示插入的整个注释。 是否可以在不硬编码单元格高度的情况下执行此操作?因为此应用程序将在多个设备上运行,并且每个设备的屏幕都不同

我已尝试在viewDidLoad中使用以下代码行使用自动布局,但未成功:

questionsTableView.rowHeight = UITableView.automaticDimension
questionsTableView.estimatedRowHeight = 390
此外,我还尝试在之前使用
questionsTableView.beginUpdates
在标签上添加注释,并在注释分配给标签后使用
questionsTableView.endUpdates
,但也不起作用

当用户运行应用程序时,我无法像这样显示单元格,如果用户添加任何注释,则无法展开单元格并显示标签:

这是我的密码:

class MainVC: UIViewController {

    // Interface Links
    @IBOutlet weak var questionsTableView: UITableView!

    // Properties
    var imagePicker: UIImagePickerController!
    var firstCommentReceived = ""
    var secondCommentReceived = ""
    var images = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        questionsTableView.rowHeight = UITableView.automaticDimension
        questionsTableView.estimatedRowHeight = 390
    }
}

extension MainVC: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! MainCell
        cell.delegate = self
        cell.configCell()
        if !firstCommentReceived.isEmpty{
            cell.firstCommentLabel.text = firstCommentReceived
        }
        if !secondCommentReceived.isEmpty{
            cell.secondCommentLabel.text = secondCommentReceived
        }
        if !images.isEmpty{
            cell.selectedImageView.image = images[0]
        }
        return cell
    }

    // Set the height of each row from UITableView
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 390 // size of the maximum height of the cell if the user add comments and images. This size should be dynamic not hardcoded.
    }
}
在这里,我用我的问题创建了一个GitHub小示例:


谢谢你,如果你正在读这个

我查看了您的项目,发现您有布局问题^”

1-您需要将单元格布局设置为从上到下的布局,我发现您没有将此设置到单元格中的最后一个元素,即图像

2-您需要找到要使用的动态标签,例如,在您的标签中,注释1和注释2具有优先级

3-最后我发现,你的单元格高度是静态的,在你的情况下,它应该是动态的

//从UITableView设置每行的高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension // in your app it was 390
}
如果您想查看,这是编辑项目代码


祝您好运;)

只需给出屏幕截图中显示的底部图像视图的底部约束,并使用设置行的高度即可

return UITableView.automaticDimension
//or you can remove this method

创建了对您的repo的pull请求。当应用程序运行时,我如何删除此空间?hmmm有很多技巧可以做到这一点,例如,您可以使用每个注释的高度常量进行控制,或者正确的方法是使用其他nib单元格:DI将标签的高度设置为24像素,因为我在控制台中收到警告。如果我删除了标签的高度,那么它是有效的,但我有很多警告。以下是警告:给我一些时间,我会为你解决这个问题,并共享屏幕。你想这样做吗?---你可以下载该项目,并接受答案,如果它为你完成()当我在标签中没有任何内容时,是否可以删除此空间?我将标签的高度设置为24像素,因为我在控制台中收到警告。如果我删除标签的高度,则我正在工作,但我有很多警告。以下是警告:我想让它们保持为空。如果我没有wro,Tableview单元格将自动设置高度ng你想要这个吗?是的。如果用户不使用相机拍照,UIImageView将不存在。如果用户添加注释/标签/照片,则显示标签/ImageView。好的。我刚刚从标签中删除了固定文本。你需要设置高度约束IBoutlet,然后将其设置为0。当用户添加任何注释时,你需要调用警报集约束的背面具有所需的任意高度。