Swift iOS UITableView

Swift iOS UITableView

Created By Debasis Das (26-Dec-2014)

In this Sample Application we will create a simple iOS App having a single plain style table view having only one section and will load the same from a NSArray.

SwiftIOSTableView3

  • Step 1: Create a new Xcode – IOS Project and choose SingleView Application Option

SwiftIOSTableView1

  • Step 2: Choose Language as Swift (At this moment switch off the core data option)

SwiftIOSTableView2

  • Step 3 : Modify the ViewController to be a subclass of UITableViewController rather than UIViewController
  • Step 4:  In the Main.storyboard , change the custom class to your UITableViewController subclass
  • Step 5: Mark the subclass as the Initial View Controller.

Replace the code in your subclass as the following

//
//  KSTableViewController.swift
//  SwiftTableView_1
//
//  Created by Debasis Das on 12/26/14.
//  Copyright (c) 2014 Knowstack. All rights reserved.
//

import UIKit

class KSTableViewController: UITableViewController {

    var dataArray:[String] = ["Debasis Das","John Doe","Jane Doe","Mary Jane","John AppleSeed"]
   
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    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.
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        
        let object = dataArray[indexPath.row] as NSString
        cell.textLabel.text = object
        return cell
    }
    
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return false
    }

}

Build and Run

SwiftIOSTableView3

The sample code can be downloaded from the following location

SwiftTableView_1

Posted in Cocoa, Generic, iOS, Swift Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news