Calculate the table's contentSize.height with custom cells of different types iphone

前端 未结 7 1150
-上瘾入骨i
-上瘾入骨i 2020-12-16 15:03

In my application , the tableview is having different types of custom cells with different heights. I want to put a view below this tableView, for which I need to calculate

相关标签:
7条回答
  • 2020-12-16 15:30

    The easiest thing I can think of would be to set your view as the table's tableFooterView

    0 讨论(0)
  • 2020-12-16 15:32

    UITableView is a subclass of UIScrollView, therefore you can use the instance method contentSize on your tableview:

    yourtableView.contentSize.height
    

    Should give you the height of your tableview content view

    0 讨论(0)
  • 2020-12-16 15:32

    I think what you're looking for is self.view.bounds.size.height.

    Check out Oliver Drobnik's detailed post on creating a pull-to-reload table view. In his case it's easier, because the view is at the top, so he checks for a negative contentOffset.

    If you want it at the bottom, you could check for scrollView.contentOffset.y > (self.view.bounds.size.height + pullToReloadViewHeight)

    0 讨论(0)
  • 2020-12-16 15:37

    This is an old question, but, today I've solved my problem with this

      func viewDidLoad() { 
          super.viewDidLoad()
    
          tableView.rowHeight = UITableViewAutomaticDimension
          tableView.estimatedRowHeight = 44 //  >= 2
        }     
    
      func getTableViewHeight() -> CGFloat{ 
          tableView.reloadData()
          tableView.layoutIfNeeded()
    
          return tableView.contentSize.height + tableView.contentInset.bottom + tableView.contentInset.top
        }
    
    0 讨论(0)
  • 2020-12-16 15:41

    If you've implemented your own custom cells, then you've probably already implemented this function in your table delegate:

    - (CGFloat)tableView:(UITableView *)tableView
               heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    Just iterate through your rows, calling this function for each one, and sum up the result.

    0 讨论(0)
  • 2020-12-16 15:45

    i have calc height in viewDidLayoutSubviews() for content view controller. Work for me (table view automatic cell dimensions);

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        let height = table.contentSize.height+table.contentInset.bottom+table.contentInset.top
        preferredContentSize = CGSize(width: 305, height: height);
    }
    
    0 讨论(0)
提交回复
热议问题