3

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

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

Community
  • 1
  • 1
Zahid Gill
  • 302
  • 3
  • 13

2 Answers2

9

I was having the same issue. i have solved it by simply passing the UITableView content height to UITabelView frame Height.

    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 viewDidAppear function of your viewController.

    override func viewDidAppear(animated: Bool) {
        UITableView_Auto_Height();
    }
Haseeb
  • 2,214
  • 1
  • 22
  • 43
2
 BAD::if(self.UITableView.contentSize.height < self.UITableView.frame.height){
 GOOD::if(self.UITableView.contentSize.height > self.UITableView.frame.height){
mackerel
  • 131
  • 2
  • 7