Create UITableView (Table View) By the use of storyboard

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) 

1
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)


1
2
3
4
5
<span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewDidLoad</span><span class="hljs-params">()</span></span> {
        <span class="hljs-keyword">super</span>.viewDidLoad()
        tableView.delegate = <span class="hljs-keyword">self</span>
        tableView.dataSource = <span class="hljs-keyword">self</span>
    }

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 

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">ViewController</span>: <span class="hljs-title">UITableViewDataSource</span> </span>{
    <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">tableView</span><span class="hljs-params">(<span class="hljs-number">_</span> tableView: UITableView, numberOfRowsInSection section: Int)</span></span> -&gt; <span class="hljs-type">Int</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-number">10</span>
    }
   
    <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">tableView</span><span class="hljs-params">(<span class="hljs-number">_</span> tableView: UITableView, cellForRowAt indexPath: IndexPath)</span></span> -&gt; <span class="hljs-type">UITableViewCell</span> {
        <span class="hljs-keyword">let</span> cell = tableView.dequeueReusableCell(withIdentifier: <span class="hljs-string">"TestTableViewCell"</span>, <span class="hljs-keyword">for</span>: indexPath) <span class="hljs-keyword">as</span>! <span class="hljs-type">TestTableViewCell</span>
        cell.labelText.text = <span class="hljs-string">"TestCell <span class="hljs-subst">\(indexPath.row + <span class="hljs-number">1</span>)</span>"</span>
        <span class="hljs-keyword">return</span> cell
    }
   
    <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">numberOfSections</span><span class="hljs-params">(<span class="hljs-keyword">in</span> tableView: UITableView)</span></span> -&gt; <span class="hljs-type">Int</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    }
   
}

 

Related Post

E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Strings String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotatio...
Introduction to Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...
Loops in java Loops in JAVA Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. Loop is used in programming ...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...

Leave a Reply

Your email address will not be published. Required fields are marked *