
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How do I create a TableView with rounded corners in iOS?
Table View is one of the most important and fundamental part of iOS application, Every iOS developer should be familiar with it.
Almost every application you see in App store use table view.
Table views on iOS display a single column of vertically scrolling content, divided into rows. Each row in the table contains one piece of your app’s content. For example, the Contacts app displays the name of each contact in a separate row, and the main page of the Settings app displays the available groups of settings
You can read more about table view here,
https://developer.apple.com/documentation/uikit/uitableview
In this post we will see how we can create table view with rounded corners.
So, let’s get started,
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “TableViewWithRoundedCorner”
Step 2 − Open Main.storyboard and add UITableView as shown below,
Step 3 − In ViewController.swift now create, @IBoutlet of tableview from Main.storyboard and name it tableView.
Step 4 − In ViewController.swift, In viewDidLoad method, add delegate and datasource to tableView as show below.
@IBOutlet var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self }
Step 5 − Open Main.storyboard and change the background color of ViewController, add prototype cell and label inside the cell as shown.
Now add one file for new tableview cell of subtype UITableViewCell and add to the same.
Now open ViewController.swift and conform to protocol and implement the delegate methods as below.
extension ViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 2 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 80 } }
Step 6 − Run the project but table view will not come with rounded corners to do so, write the below code in view did load method.
tableView.layer.cornerRadius=10 //set corner radius here tableView.layer.backgroundColor = UIColor.cyan.cgColor
For complete code −
import UIKit class ViewController: UIViewController { @IBOutlet var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.layer.cornerRadius=10 //set corner radius here tableView.layer.backgroundColor = UIColor.cyan.cgColor } } extension ViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) → Int { return 2 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) → UITableViewCell { let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) → CGFloat { return 80 } }
- Related Articles
- How do I create a listView with rounded corners in Android?
- How to create EditText with rounded corners in Android?
- How to create NSIndexPath for TableView in iOS?
- How to create CSS3 Rounded Corners?
- Create a padded grey box with rounded corners in Bootstrap
- How to display an image with rounded corners on iOS App using Swift?
- How to add rounded corners to a button with CSS?
- How to make custom dialog with rounded corners in android?
- CSS3 Rounded corners
- How to create a TableView in JavaFX?
- How to make an ImageView with rounded corners on Android App?
- How to change background color of TableView items on iOS?
- How to make an ImageView with rounded corners on Android App using Kotlin?
- How do I stop a MySQL decimal field from being rounded?
- How to make the corners of a button round in iOS?
