如何限制分割视图控制器左侧的表视图的大小?

问题描述:

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

我抓住了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