SWIFT UITableview 根据内容设置自动高度 - XCode - iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33154875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:03:40  来源:igfitidea点击:

SWIFT UITableview set autoheight according to contents - XCode - iOS

iosxcodeswiftuitableview

提问by Zahid Gill

Automatically adjust the height of UITableViewaccording to Contents Dynamically. I tried the following solution but didn't work for me:

UITableView根据内容动态自动调整高度。我尝试了以下解决方案,但对我不起作用:

dispatch_async(dispatch_get_main_queue()) {
        //This code will run in the main thread:
        CGRect frame = self.tableView.frame;
        frame.size.height = self.tableView.contentSize.height;
        self.tableView.frame = frame;
    }

SOURCE

来源

回答by Haseeb

I was having the same issue. i have solved it by simply passing the UITableViewcontent height to UITabelViewframe Height.

我遇到了同样的问题。我已经通过简单地将UITableView内容高度传递给UITabelView框架高度来解决它。

    func UITableView_Auto_Height()
    {
        if(self.UITableView.contentSize.height < self.UITableView.frame.height){
            var frame: CGRect = self.UITableView.frame;
            frame.size.height = self.UITableView.contentSize.height;
            self.UITableView.frame = frame;
        }
    }

Call the above function in viewDidAppearfunction of your viewController.

viewDidAppear您的viewController.

    override func viewDidAppear(animated: Bool) {
        UITableView_Auto_Height();
    }

回答by mackerel

 BAD::if(self.UITableView.contentSize.height < self.UITableView.frame.height){
 GOOD::if(self.UITableView.contentSize.height > self.UITableView.frame.height){