• Blog
  • About
  • Contact
  • Blog
  • About
  • Contact
swiftress

All about the tableviews

9/9/2015

0 Comments

 
Most apps are built on tableviews, so it's an important concept to grasp.
Pull a tableview into your storyboard. Not a tableViewController mind you, that creates a new view controller. We want to add a table to our existing controller. 
Create your outlet...
         @IBOutlet weak var tableView: UITableView! 
In your class: ViewController, you need to add 
         UITableViewDelegate, UITableViewDataSource
Then in the viewDidLoad() we need to set them up...
         tableView.delegate = self
         tableView.dataSource = self
Create an array to populate our table...
            var items: [String] = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
We need to create two separate tableViews to make this work, cellForIndexRow and numberOfRowsInSection. The cells tell us what is going to be put in each cell. The rows tell us how many rows there will be in our table. 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // creates the tableviewcell 
        var cell = UITableViewCell()    
        //put's the strings we created earlier into a label
        cell.textLabel?.text = items[indexPath.row]   
        // brings up the tableview cell
        return cell                    
 }

Then we need to call up the number of Rows that will be in our table...
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
        //returns the number of items in our array we first created
    }
 
And there you have a basic table. 
0 Comments



Leave a Reply.


       

    Girl loves her some Swift.

    Archives

    September 2015

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.