2016-05-31 60 views
1

enter image description here我有一個UISplitViewController。所以我想看看我的主視圖(即表視圖)的內容大小(認爲它只有3個元素)。 我試着用self.tableview.contentsize但沒有成功。請幫我找到解決辦法。如何限制分割視圖控制器左側的表視圖的大小?

每個單元格的高度是44。 這是我寫的代碼。

class AccountTableViewController: UITableViewController { 
var filterList = [String]() 
var selectedIndex = -1 

override func viewDidLoad() { 
    super.viewDidLoad() 
    filterList = ["All Accounts","Business Accounts","Person Accounts"] 

    self.tableView.contentSize = CGSize(width: 600, height: 44*3); 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 3 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("accountCell", forIndexPath: indexPath) 

    cell.textLabel?.text = filterList[indexPath.row] 
    return cell 
}} 
+0

請張貼,顯示代碼的創建和初始化表視圖。此外,你的細胞有多高? – thephatp

+0

@thephatp 請檢查編輯的內容並嘗試解決我的問題。 –

+0

所以爲了澄清,你不想在列表中的三個項目之後看到左側視圖中的行?或者你想利用這個空間來做其他事情? – thephatp

回答

3

我抓住了Ray Wenderlich的示例項目(https://www.raywenderlich.com/94443/uisplitviewcontroller-tutorial-getting-started)作爲起點。這就是你在下面的圖片中看到的修改。

在Interface Builder中,首先要確保你的設置使用自動版式: enter image description here

然後拖動UIView原型細胞下(我將背景設置爲橙色,所以你可以看到它在哪裏),使大小不管你喜歡: enter image description here

我加了一些田間地頭到我們剛創建的新UIView,我添加的約束,所以你可以看到他們根據在運行時的內容移動。 (你會注意到故事板視圖控制器作爲IB更大的寬度比在運行時)這是我補充的約束: enter image description here

最後,運行應用程序 - 無需在代碼中做任何事情: enter image description here

一旦你得到了這個基本部分的工作,那麼你可以在代碼中配置視圖的大小。您可以根據需要在IB中設計視圖,或者在代碼中進行設置 - 無論您喜歡什麼。

下面是其中的表格只有一行的例子:

enter image description here

這裏與表中沒有行的例子:

enter image description here

+0

謝謝@thephatp 別再問問題了。想法正在工作。 –

+0

這實際上是部分工作。當沒有行時,視圖顯示的是屏幕的一半。可能會有額外的限制要求@thephatp –

+0

@RamcharanReddy可能是內容壓縮或內容擁抱,你已經改變的東西?我拿起了上週修改的項目來獲取上面的截圖,它對我來說工作得很好。我會在答案中再添加兩個圖片來向你展示。抓住我的答​​案頂部的項目,並進行我在這裏提到的修改,看看你是否得到了我所做的相同結果。 – thephatp

相關問題