XCode6 BETA iOS8 - Create Simple UITableView with Swift Language

This is my first tutorial in Swift Language for iOS8 with XCode6 BETA, and i’m going to use UITableView with Swift language.





Open XCode6 BETA and new “Single View Application” choose swift language to start.



Open MainStoryBoard file drag object UITableView from the object library put on your ViewController.


Setup in ViewController



Look at Attribute Inspector insert a sample prototype cell to “1” :



and click on the cell in TableView identified with “Cell” like the image below.


Setup a Delegate and DataSource options for UITableView with the ViewController icon (yellow one, hold the CTRL Button and then drag the TableView to the yellow icon).



Open ViewController.swift from the project tree and modify the class from:
import UIKit

class ViewController: UIViewController{
to this:
import UIKit

class ViewController: UIViewControllerUITableViewDelegate{
Insert two new function after method ViewdidLoad() like this:
//Table
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
    }
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell{
    }

Put the return number of your rows.

//Table    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{        return 10    }

And create an instance of your TableViewCell like Title and Subtitle with a cell style.

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell{        let tableCell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")                tableCell.text = "Title of Row: #\(indexPath.row)"        tableCell.detailTextLabel.text = "Detail of Row: #\(indexPath.row)"                return tableCell    }

Check the source code in ViewController.swift
          //  ViewController.swift
//  TestSwift
//
//  Created by PERSONAL on 6/11/2014 BE.
//  Copyright (c) 2557 Daydev Co., Ltd. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDelegate{                          
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.
    }
  
    //Table
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
        return 10
    }
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell{
        let tableCell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")      
        tableCell.text = "Title of Row: #\(indexPath.row)"        tableCell.detailTextLabel.text = "Detail of Row: #\(indexPath.row)"      
        return tableCell
    }
}

Ok last step “Run” your Application and see a result, enjoy!



My Origin article in thai: iOS Developer

Comments