2

enter image description hereI have a UISplitViewController. So I want to see my master view(i.e table view) upto the size of its content (consider it have only 3 element's). I tried With self.tableview.contentsize but did not succeeded. Please help me finding solution.

Each cell's Height is 44 . This is the Code That I wrote.

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
}}
Ramcharan Reddy
  • 621
  • 1
  • 5
  • 18

1 Answers1

3

I grabbed Ray Wenderlich's example project (https://www.raywenderlich.com/94443/uisplitviewcontroller-tutorial-getting-started) as a starting point. That's what you see modified in the pictures below.

In Interface Builder, first make sure your set to use AutoLayout: enter image description here

Then drag a UIView under the prototype cell (I set the background to orange so you can see where it is), and make the size whatever you like: enter image description here

I added some fields to the to the new UIView that we just created, and I added constraints so you could see them move based on the content at runtime. (You'll notice the storyboard view controller as a much larger width in IB than at runtime.) Here's the constraints I added: enter image description here

Finally, run the app--no need to do anything in code: enter image description here

Once you get this basic part working, then you can configure the size of the view in code. You can design the view in IB if you want, or do it in code--whatever you prefer.

Here's an example with just one row in the table:

enter image description here

And here's an example with no rows in the table:

enter image description here

thephatp
  • 1,479
  • 1
  • 20
  • 37
  • Thanks @thephatp No More questions. Idea is Working. – Ramcharan Reddy Jun 02 '16 at 06:33
  • This is actually working partially. When there are no rows the view is displaying upto half of screen. there might be an extra constrain required @thephatp – Ramcharan Reddy Jun 09 '16 at 05:30
  • @RamcharanReddy might be something with content compression or content hugging that you've changed? I pulled up the project I modified last week to get the screenshots above, and it's working fine for me. I'll add two more pics to the answer to show you. Grab the project at the top of my answer and make the modifications I mention here and see if you get the same results I did. – thephatp Jun 09 '16 at 13:43
  • Please try with out any cells @thephatp – Ramcharan Reddy Jun 09 '16 at 13:46
  • @RamcharanReddy already did. See screenshot above. Try grabbing the project I linked to and make the changes in there and see if you can get the results I got. If so, you know there's something in your current constraints that is causing the undesired behavior. – thephatp Jun 09 '16 at 13:49
  • Ok Thanks you @thephatp I'll check what leads me to that logical error at my end. :) – Ramcharan Reddy Jun 09 '16 at 13:54