How to use UITableView in Above IOS 8 and Swift (UITableView)

In this tutorial we are study how to use the TableView in Swift. One of the most common View which are present in almost all apps is UITableView. For use of table view you need to have knowledge about how to write code in swift and swift syntax. we are assuming that you know how to write code in swift.
For creating the table view have two methods
1: Creating by using of code
2: creating by using of interface builder
By using of interface builder we have different techniques like

  • Use of storyboard
  • Use of xib
  • StoryBoard with custom cell

Using of interface builder is a common and easy method, Now we are study UITableView by storyborad (Interface builder) step by step.
Follow these steps for UITableView by the use of storyboard in xcode :

1 Create new project

open your xcode and create a new project

2 Select single view application

select the single view app in xcode and select your product name (Project name) like (AGTVStoryboardExample)

3 Add Table view in storyboard predefine view Controller

(OR add new view controller we are study later)
Xcode open your project in front of you. Now For creating the tableview list by storyboard Go to main.storyboard file and select the “select your library” tab at right bottom cornor of this file. Find table view in this listing and after that drag and drop of your tableview on view controller.

4 Add constraint to Table view

After drag and drop of table view, select the tableview and go to Right bottom of this file and select the “Add new constraint” tab and assign the constraint to table view.

5 Create IBOutlet and Attach Table view delegate and dataSource

Now open the Right Cornor tab “show the assitant editor” and create the @IBOutlet for table view like
image

After that attch the tableview delegate and datasource by storyborad. (It’s also done by code)

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

6 Now create the Table view cell

For create the table view cell, Drag and drop TableViewCell from “Show the object libaray” tab and add the UI according to you. After that create a new “cocoa Touch class” swift file with subclass of UITableViewCell .

7 Assign cell class and create identifier

Now assign your cell class into your table view cell in storyBoard and give the cell identifier name according to you (Try to give same name as class name)

8 Implement CollectionView DataSource methods

Finaly implement the table view DataSource methods, and return the number of rows , number of sections and cell for table view . In cellForRowAt method assign the values according to you.

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell
        cell.labelText.text = "TestCell \(indexPath.row + 1)"
        return cell
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
}

 

Related Post

Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
Dart Data type | Dart Tutorial Dart Data type Variables can store data of different types, and different data types can do different things. Dart supports the following data typ...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use of xib in swift 3.1 and XCode 8.3.2 . Custom views are common ...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...
Author

Write A Comment