• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Tutorial – Custom UITableView Cell

Home swift Tutorial – Custom UITableView Cell

Tutorial – Custom UITableView Cell

Jan 1, 2015 | Posted by Andrew | swift, tutorial, xcode |

Today we are going to look at creating a custom UITableView Cell.

I am going to start off from the post here on creating a tableview to get things going. So initially my project will already be setup with a basic tableview that looks as follows:

Screen Shot 2015-01-01 at 11.44.27 am

 

 

The code in our ViewController looks like this currently:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    
    
    var tableData: [String] = ["Hello", "My", "Table"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        tableData.append("Seemu")
        self.tableView.reloadData()
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 	{
     
        let cell: UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
        
        cell.textLabel?.text = self.tableData[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Row \(indexPath.row) selected")
    }

}

By the end of this post we will make it so each car manufacturer has a image next to their name, we will achieve this by making a custom cell that allows us to set it up however we like.

The first thing to do is to setup our custom view for our custom cell. To do this go File->New->File. Select “User Interface” under iOS, select empty and hit next. We will save it as vwTblCell. This gives us a blank interface to play around with to create our tableview. From the bottom right corner find the Table View Cell control and drag it on to our interface. Once this is done drag a label and also UIImage onto the Table View Control we made.

It should look like the following:

Screen Shot 2015-01-01 at 11.53.15 am

Once this is done select the attributes inspector up the right hand side, for identifier type in “cell”.

Now that this is done we need to setup an accompanying class for our interface to connect the code & label and image up. Go File->New-File. Select Source under IOS and Cocoa Touch Class. Name the class TblCell and ensure it is a subclass of UITableViewCell. Go back to the vwTblCell and set the custom class to TblCell.

Screen Shot 2015-01-01 at 12.04.44 pm

 

Now select the assistant editor to split the view assisteditor. Click on the label, hold it and control and drag it into our custom TblCell class. Create a new outlet named imgCarNane. Repeat the same steps for the Car Manf Name label but name it lblCarName. It will look like the following:

Screen Shot 2015-01-01 at 12.08.06 pm

 

Now we will add some car logo images to show in our UIImage view, you find find them on google and download them. Add them in as follows with these names, in our Images.xcassets.

Screen Shot 2015-01-01 at 12.11.37 pm

 

Now go to the ViewController.swift and make changes to the code so it looks like the following:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet var tableView: UITableView!
    var tableData: [String] = ["Ferrari", "BMW", "Mitsubishi", "Lambo"]
    
    // 1
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register custom cell
        let nib = UINib(nibName: "vwTblCell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: "cell")
    }
    
    // 2
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData.count
    }
    
    // 3
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 	{
        let cell:TblCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! TblCell
        cell.lblCarName.text = tableData[indexPath.row]
        cell.imgCarName.image = UIImage(named: tableData[indexPath.row])   
        return cell
    }
    
    // 4
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Row \(indexPath.row) selected")
    }
    
    // 5
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
  
}

The code does the following:

// 1

This sets up & loads our custom TableView Cell.

// 2

Returns the number of car manufacturers to display in the table view.

// 3

First of all this loads our custom cell & the class TblCell we made. It then sets the TblCell image and text to be the car manufacturer, remember we connected these up from our custom view!

// 4

Shows what row of the tableview was selected

// 5

When making the custom view if we resize the view in our interface builder you will notice it doesn’t keep the right size. If you want to make your table view cell bigger you must also set the height here.

Now you can run the application and you should see your custom table cells as follows:

Screen Shot 2015-01-01 at 12.19.36 pm

 

So the table cell can be customised in any way, not just with image views and labels! You can also add buttons, text fields and many other objects to your custom cell.

DownloadSourceCode

Tags: uitablecelluitableview
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift basic UITableView Tutorial

Oct 7, 2014

In this post we will look at implementing a basic[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next