iPhone App Development: Set Image In Table View Cell - Day Three

Before reading this article, please go through the following articles

Introduction

In this article I am continuing the table view topic and today we will see how we can make the categories in table view and how we can give the image and subtitle.  I am following the previous project and we will follow this at the end of the table view project.

Step 1

Now Click on Storyboard and then click on table view and in the left side you will see style dropdown; select subtitle. After this run the project and youwill see that the subtitle appears.

subtitle

When you run the project you see in each cell the heading ofthe subtitle occurs with the course name; now we can set the author name here.

course

Give The Subtitle Name Using Array

Now I will give the name in the subtitle in which I implement the only line of code, which is
Cell.detialTextlabel?.text=courseAuthor.

Code
  1. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell  
  2. {  
  3.     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell  
  4.     if indexPath.section == 0  
  5.     {  
  6.         let (courseTitle, courseAuthor) = devCourses[indexPath.row]  
  7.         cell.textLabel ? .text = courseTitle  
  8.         cell.detailTextLabel ? .text = courseAuthor  
  9.     }  
  10.     else  
  11.     {  
  12.         let (courseTitle, courseAuthor) = webCourses[indexPath.row]  
  13.         cell.textLabel ? .text = courseTitle  
  14.         cell.detailTextLabel ? .text = courseAuthor  
  15.     }  
  16.     return cell  
  17. }  
When you run the app you will see that the name appears in the subtitle and remember that you see that the author is the same in all cells because in both arrays I gave one author name and copy pasted it in all of them; in this way it shows the same.

author

Set Image In Cell.

Move to asset folder and click on plus button which appears in the bottom and then click on New Image Set; after this set any name of image set and then drag and drop the images. In the left side you see that star images are appearing, now I drag this image into IDE star.

asset folder

Now go to the viewController.swift file and edit using the following method. In the bottom of this method you see that we change only two to three lines and our image is displayed and you see that our image is the same because we set only one image. Futhermore in our next article we will continue to work on Table view.
  1. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell  
  2. {  
  3.     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell  
  4.     if indexPath.section == 0  
  5.     {  
  6.         let (courseTitle, courseAuthor) = devCourses[indexPath.row]  
  7.         cell.textLabel ? .text = courseTitle  
  8.         cell.detailTextLabel ? .text = courseAuthor  
  9.     }  
  10.     else  
  11.     {  
  12.         let (courseTitle, courseAuthor) = webCourses[indexPath.row]  
  13.         cell.textLabel ? .text = courseTitle  
  14.         cell.detailTextLabel ? .text = courseAuthor  
  15.     }  
  16.   
  17.     let myimage = UIImage(named: "CellIcon")  
  18.     cell.imageView ? .image = myimage  
  19.   
  20.     return cell  
  21. }  
output


Similar Articles