auto layout etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
auto layout etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

3 Şubat 2015 Salı

Dynamic TableView Height using AutoLayout in Swift









iPhone has different sizes. In order to a tableview's height is suitable for any iPhone size, we can use autolayout.

An examle for Dynamic TableView Height using AutoLayout in mobile development

1. Add a tableview in view and implement UITableView Delegate and Data Source








class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
...




2. Define tableview width

For Tableview width = iPhone width , we should define "equals width"



3. Add top and bottom space constraints

For Statusbar space, we should define "20"  top space
For horizontal space and vertical bottom space,  we should define "0". Since we define bottom space  as  "0" , tableview height has dynamic height.


4.  Control the storyboard preview for different iPhone sizes




5. Add an outlet object for TableView



5. Register tableview class


Add register class method in viewDidLoad

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")


6. Add tableview methods for testing

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        
        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel.text = "item \(indexPath.row)"
        
        
        
        return cell
    }
    
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("Tablecell #\(indexPath.row)!")
    }


7. Result for iPhone 6




You can download from https://github.com/ozlemakalin/DynamicTableViewHeightSwift