2016-07-06 125 views
0

林很新的iOS開發,我一直在堅持這幾天動態設置的UIScrollView與UITableView的高度由它的內容

我有一個UIScrollViewUITableViewUIImageView,在UIImageView在頂部的表和高度是靜態的,所以我沒有任何問題,這個問題是UITableView,我需要根據內容動態獲取高度,並將該高度設置爲表的高度約束和內容的scrollview。

這些單元格是自我大小的,所以單元格的高度根據內容而不同。我能做到的最好的辦法是使用的contentSize.height財產獲得高度,但顯然這會返回基於我設置的estimatedRowHeight的高度,而不是每行的實際高度。

代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.dataSource = self 
    tableView.delegate = self 
    tableView.estimatedRowHeight = rowHeight 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.layoutIfNeeded() 
    tableHeight.constant = tableView.contentSize.height 
    scrollView.contentSize.height = imageView.bounds.height + tableView.contentSize.height 
} 

我已經試過:

  • 通過使用UITableViewcontentSize.height財產獲得的高度,如圖中的代碼和之前解釋。
  • 獲取每個行的高度,分別在willDisplayCell上,並將其添加到表格的高度約束和scrollview的contentSize中。但看起來這樣會增加更多的行數,給我一些表格末尾的空單元格。內部willDisplayCell的代碼:

    let cellHeight = cell.bounds.height 
    tableHeight.constant += cellHeight 
    scrollView.contentSize.height += cellHeight 
    
  • 的以前一樣,除了i以外具有相同的結果內cellForRowAtIndexPath試過。

+0

第一個問題,爲什麼在滾動視圖內使用一個表(它有一個內置的滾動視圖)(並設置高度來取消表的滾動功能)?其次,爲了做你所要求的,你將需要在for循環中手動調用'cellForRoAtIndexPath'遍歷你的表數據。然後獲取從該函數返回的每個單元格的高度,併合並所有高度以獲得全局表格高度。但是我回到第一個問題,爲什麼要重做已經爲你做的事情(在scrollview中滾動的表)? – Putz1103

+0

**訪問此內容爲您的答案** http://stackoverflow.com/questions/5628527/dynamically-set-uiscrollview-height-and-uitableview-height-depending-on-content –

回答

4

Apple documentation阻止一個UIScrollView中嵌入UITableView

你不應該嵌入的UIScrollView對象的UIWebView或UITableView的對象。如果這樣做,可能會導致意外的行爲,因爲兩個對象的觸摸事件可能混淆並被錯誤地處理。

要解決您的問題,您可以將UIImageView放在標題視圖中,或更改第一行的單元格以顯示圖像。然後,您將使用您的UITableViewDelegate,特別是tableView:heightForRowAtIndexPath:來調整單元格高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // adjust the return based on the indexPath 
    return 30; 
} 
0

我有同樣的問題,我找到了解決方案。我在UIScrollView中有一個TableView,不知道如何動態設置UIScrollView的高度。首先你必須設置你的scrollView,你可以在stackoverflow(How to use UIScrollView in Storyboard)這裏查看它。下一步是將tableView的約束設置爲scrollView(它應該是tableView的superView):前導,尾隨和底部(底部爲0,其他爲你想要的),以及最接近的對象的頂部約束,並且不管你想要哪個。 所以現在你必須動態設置UIScrollView的高度。它取決於2件事:設備屏幕的高度和tableView的高度(因爲tableView的內容是動態的)。 你必須使用這個擴展,使設備決心更加舒適:iOS: How to determine the current iPhone/device model in Swift? 這些事情我這樣做後:

var viewHeight: CGFloat = 0 

var modelDevice: String { 
    get { 
    return UIDevice.currentDevice().modelName 
    } 
} 

var tableViewHeight: CGFloat { 
    get { 
    // 90 is my tableViewCell's row height 
    return CGFloat(detail.cart.count) * 90 
    } 
} 

func configureSelfHeight(modelDevice: String) { 
    switch modelDevice { 
    case "iPhone 4", "iPhone 4s": viewHeight = 480 
    case "iPhone 5", "iPhone 5c", "iPhone 5s", "iPhone SE": viewHeight = 568 
    case "iPhone 6", "iPhone 6s", "iPhone 7": viewHeight = 667 
    case "iPhone 6 Plus", "iPhone 6s Plus", "iPhone 7 Plus": viewHeight = 736 
    default: viewHeight = 568 
    } 
    scrollView.contentSize.height = viewHeight + tableViewHeight 
} 

並調用viewDidLayoutSubviews()內這種方法:

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    configureSelfHeight(modelDevice) 
} 

希望它會幫助你。:)