Create UITableView using storyboard in Swift 4

This example to learn UITableView using storyboard. the previous example study to Create UITableView Programatically swift 4.




First of all create a new project : New Project

Step : 1
After create new project, Go to the UIViewController file and Create a instance of UITableView. also gives delegate and datasource.
class ViewController: UIViewController  , UITableViewDelegate , UITableViewDataSource{
@IBOutlet var tblList : UITableView!

Step : 2
Go to the storyboard , drag and drop UITableView in the storyboard, also give IBOutlet connection with tableview in storyboard.



Step : 3
Create class of UITableViewCell with name "CellData".  Add the UILabel with name "lblName". The Code of the class.


import UIKit

class CellData: UITableViewCell {

    @IBOutlet var lblName : UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Step : 4
After create class of UITableViewCell. drag and drop UITableViewCell and also add the UILabel inside the Cell.




Step : 5
Give the UITableViewCell custom class name "CellData".



Step : 6
Add the IBOutlet Connection of lblName. 



Step : 7
gives the identifier of UITableViewCell.



Step : 8
The full code of the UIViewController.


import UIKit

class ViewController: UIViewController  , UITableViewDelegate , UITableViewDataSource{

    @IBOutlet var tblList : UITableView!
    let data: [String] = ["kirit", "kevin", "hitesh"]



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CellData = tableView.dequeueReusableCell(withIdentifier: "CellData") as! CellData
        cell.textLabel?.text = self.data[indexPath.row]
        return cell
    }

}

Output 



Full code of the demo : download here


Thanks.

Create UITableView using storyboard in Swift 4 Create UITableView using storyboard in Swift 4 Reviewed by Kirit Modi on 22:39:00 Rating: 5

No comments:

Powered by Blogger.