Monday, September 29, 2014

UITableView using swift iPhone example

In this article we create UITableView using swift programming language. For create UITableView in iPhone main class is UITableViewController, UITableViewDelegate, UITableViewDataSource.


To create UITableView below two overrided method is used in ViewController

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    }
The above method is used for the number of cells in UITableView.

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    }

This method is used for the create cell at particular row and data to be display in cell.

Below is the full TableViewController class

import Foundation
import UIKit

class tableViewContoller: UITableViewController, UITableViewDelegate, UITableViewDataSource {
    let kCellIdentifier: String = "Cell"
IBOutlet var appsTableView : UITableView!
    let array:NSArray = ["Item 1","Item 2", "Item 3","Item 4"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier)
        cell.textLabel?.text = "\(array[indexPath.row])"
        cell.textLabel?.textColor = UIColor.blueColor();
        return cell
    }
}
 Output of the above UITableView Controller.

Hope this is post is helpful for you...

No comments:

Post a Comment