UITableViewController in Swift 4.

UITableViewController that specializes in managing a table view. we are study how to use UITableViewController with data. How to Create UITableView Programatically Swift 4.




Step: 1
First of all you create a new demo project for UITableViewController. So you see the "How to create project for iOS using Xcode"

Step: 2
Delete the ViewController.swift that was created automatically. Also go to main.storyboard and delete the ViewController. you follow the below image.


Step: 3
Drag and drop the UITableViewController from the object library to storyboard.



Step: 4
After adding a UITableViewController in storyboard. Add a new file which parents class is also UITableviewController.




Step: 5
Give the name of the file is "FruitsTableVC". 



Step: 6
The full code of the "FruitsTableVC" in which include the data and tableview Delegate and DataSource method. 
import UIKit

class FruitsTableVC: UITableViewController {

    var fruitsData = ["Apple", "Banana", "Banana", "Apple" , "Apple" , "Banana"]

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruitsData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

        let fruitName = fruitsData[indexPath.row]
        cell.textLabel?.text = fruitName
        cell.detailTextLabel?.text = "Fruit..!"
        cell.imageView?.image = UIImage(named: fruitName)
        return cell
    }

}

Step: 7
Now open the storyBoard and set "FruitsTableVC" in the Custom Class.



Step: 8
There is only one view controller in storyboard so that you set FruitsTableVC as init view controller.


Step: 9
Select TableView Cell and gives identifier name "LabelCell" and also select style as subtitle.



OUTPUT:




Download full code : here

Create UITableView programatically in swift 4.

Thanks.

UITableViewController in Swift 4. UITableViewController in Swift 4. Reviewed by KIRIT MODI on 04:32:00 Rating: 5

2 comments:

Powered by Blogger.