ios, swift, iphone development tutorials, x-code development

Powered by Blogger.

Main Menu

create a TableView in ios swift xcode

create a TableView in ios swift xcode It is very simple to create a table view in ios swift.But first you know that how to create a ne...

Sunday, 20 May 2018

create a TableView in ios swift xcode


create a TableView in ios swift xcode


It is very simple to create a table view in ios swift.But first you know that how to create a new project in X-Code if you dont know then just see the last post.

before creating a tableView we learn what is table view and why we used it?

Table is used to show the data in sequential way.It gives us a vertically scroll view which consist of a number of cells(generally reusable cells). In table view some majorly  func(Function) are must used.
              1) NUMBER OF ROWS IN SECTION its stand for row in table how many rows are created in tableVIew.
                              func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
                                           {
                                             return Code
                                           }

               2) CELL FOR ROW AT INDEXPATH  this function is used to display the data in cell indexpath.

                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
                        {
                                 return code
                         }
for better experience we can do it practically with example :

1. First create a x-code project.
2. after creating a project select MAIN.storyboard(its use for creating user interfaces) and select Show the object library which is shown in the image.
Show the object library
Show the object library 


3. And search the UITableVIew and drag and drop in the View Controller Scene.
UITableVIew
UITableVIew
4. After taking the tableView first we set the constraints for auto set on all iphones.

Add Constraints
Add Constraints
5. Now set the Prototype Cell 1.
set prototype cell
set prototype cell
6. create a cocoa Touch Class file gives name which you want to give example like:- TableViewCell.swift.
 cocoa Touch Class
 cocoa Touch Class
7. Select the subclass of UITableViewCell and click next.

subclass of UITableViewCell
subclass of UITableViewCell
8. select the Identity Inspector and set the custom class as your subclass of UITableViewCell name.
Identity Inspector
Identity Inspector

9. Now drag and drop the the lable in prototype cell from object library. for showing the data.


Drag and Drop Lable
Drag and Drop Lable
10. After drag and drop the lable set the constraints of lable in prototype cell.
11. create outlet for the lable in your Subclass of uitable view cell.

12. Code of TableViewCell(subclass of UITableViewCell).

//
//  TableViewCell.swift
//  TableViewExample
//
//  Created by MICROLET on 20/05/18.
//  Copyright © 2018 MICROLET. All rights reserved.
//

import UIKit

class TableViewCell: UITableViewCell {

    @IBOutlet weak var lableName: 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
    }

}

13. code for the table view functions is:-

//
//  ViewController.swift
//  TableViewExample
//
//  Created by MICROLET on 18/05/18.
//  Copyright © 2018 MICROLET. All rights reserved.
//

import UIKit

class ViewController: UIViewControllerUITableViewDelegateUITableViewDataSource {
    
    
    var names = ["Aviansh""Kapil""Muskan""Renu""Nisha""Lalit""Tammy""Jully"]
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     
        let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) asTableViewCell
        cell.lableName.text = self.names[indexPath.row]
        return cell
    }
    

    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.
    }


}

14. After writing this code just build and run the app in your simulator and your output is boomb..!!

Output gadget simulator
Simulator
Thanks for reading this post Hope you enjoyed this post and know abot how to create the TableView in IOS SWIFT X-CODE.
















  

0 comments:

Post a Comment