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.
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.
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
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell
- {
- let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
- if indexPath.section == 0
- {
- let (courseTitle, courseAuthor) = devCourses[indexPath.row]
- cell.textLabel ? .text = courseTitle
- cell.detailTextLabel ? .text = courseAuthor
- }
- else
- {
- let (courseTitle, courseAuthor) = webCourses[indexPath.row]
- cell.textLabel ? .text = courseTitle
- cell.detailTextLabel ? .text = courseAuthor
- }
- return cell
- }
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.
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.
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.
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell
- {
- let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
- if indexPath.section == 0
- {
- let (courseTitle, courseAuthor) = devCourses[indexPath.row]
- cell.textLabel ? .text = courseTitle
- cell.detailTextLabel ? .text = courseAuthor
- }
- else
- {
- let (courseTitle, courseAuthor) = webCourses[indexPath.row]
- cell.textLabel ? .text = courseTitle
- cell.detailTextLabel ? .text = courseAuthor
- }
-
- let myimage = UIImage(named: "CellIcon")
- cell.imageView ? .image = myimage
-
- return cell
- }