Swift iOS Custom UITableViewCell

 

Swift iOS Custom UITableViewCell

Created By: Debasis Das (26-Dec-2014)

In this article we will create a simple iOS Application in Swift for having a Custom UITableViewCell that has 3 UI Labels and 2 UIImageViews.

Output of the application will be as the image given below

SwiftCustomUITableViewCell2

For this app we will use the Master – Detail Application template from xCode

SwiftCustomUITableViewCell1

From the template project we will remove the detail view controller  and will remove the same from the Main.storyboard.

We will modify the Prototype Cell to include the following

  •  UILabel in the TableView–>Cell–>ContentView–> UILabel 1
  •  UILabel in the TableView–>Cell–>ContentView–> UILabel 2
  •  UILabel in the TableView–>Cell–>ContentView–> UILabel 3
  •  UILabel in the TableView–>Cell–>ContentView–> UI Image View 1
  •  UILabel in the TableView–>Cell–>ContentView–> UI Image View 2


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

import UIKit

class MasterViewController: UITableViewController {

    var objects = NSMutableArray()
    var dataArray = [["firstName":"Debasis","lastName":"Das","email":"debasis_das@knowstack.com"],["firstName":"John","lastName":"Doe","email":"jdoe@knowstack.com"],["firstName":"Jane","lastName":"Doe","email":"janedoe@knowstack.com"],["firstName":"Mary","lastName":"Jane","email":"mjane@knowstack.com"]]
 
    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table View

    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 NSDictionary
        (cell.contentView.viewWithTag(10) as UILabel).text = object["firstName"] as? String
        (cell.contentView.viewWithTag(11) as UILabel).text = object["lastName"] as? String
        (cell.contentView.viewWithTag(12) as UILabel).text = object["email"] as? String
        (cell.contentView.viewWithTag(100) as UIImageView).image = UIImage(named: "person.png")
        (cell.contentView.viewWithTag(200) as UIImageView).image = UIImage(named: "remove.png")
        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
    }
}

The sample code can be downloaded from the following location

CustomUITableViewCell

Posted in iOS, Swift Tagged with: , , , ,
One comment on “Swift iOS Custom UITableViewCell
  1. Jorge Merino says:

    Great example, thanks for sharing it.

    I’ve downloaded the code and it won’t work with Xcode version 7 and swift 2. The converter was not able to work through it. I’ve just wanted to let you know in case you didn’t notice earlier.

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