Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » UITableView tutorial using Swift in iOS

UITableView tutorial using Swift in iOS

August 19, 2022 by Source Freeze 3 Comments

In the previous example we have learned about how to create uitextfield and uitextfielddelegate, Now this tutorial we will teach about how to create iOS uitableview tutorial in swift.

uitableview is one of the most used ios control. It is used to list the set of items in a single column and it is a subclass of uiscrollview so by default the scrolling is enabled although uitableview allows vertical scrolling only. If you are serious documentation lover please refer the apple documentation here for the detailed information. uitableview is close identical with uicollectionview and you can see the example uitableview in the ios settings app.

Open Xcode and create a new “Single View Application”, enter the product name as “UITableView Tutorial” next fill out the Organization Name and Organization identifier then select “Swift” language and device family as iPhone and  remains core data field as unchecked because we are not using that feature.

uitableview tutorial in swift

UITableView : Designing Part

Now, select the Main.storyboard file. Find the Table View object from “Show the Object library”

Table View Object in Object library

Drag and drop the UITableView to ViewController scene now the viewcontroller  will looks like below.

Drag and Drop the UITableView to Storyboard

Next, Create the IBOutlet for UITableView and name the tableview object as “simpleTableView”

UITableView Outlet

Select the ViewController Root View, then connect datasource and delegate to the UITableView as show in the below.

uitableview delegate and datasource connection

To ensure the connection are properly connected

UITableView : Coding Part

Next, Download and include this images into your project, then add TableView delegate and datasource protocol declaration in the ‘ViewController.swift’ file as shown in the below. you are getting error while adding, yes for UITableViewDataSource needs to implement two mandatory methods that will be explained later in this tutorial.

UITableView Delegate and Datasource Protocal declaration

Add the below code into the viewcontroller class file, here i am declaring the two string array one is named as animalNames, which contain animal names list and another array named as animalImages which contain animal image names.

var animalNames: [String] = ["Cat", "Dog", "Elephant", "Gangaroo", "Hipopotamos", "Lion", "Zebra"]
var animalImages: [String] = ["Cat", "Dog", "Elephant", "Gangaroo", "Hipopotamos", "Lion", "Zebra"]

Next, we have to add the two datasource methods tableView:numberOfRowsInSection and tableView:cellForRowAtIndexPath. It’s mandatory to implement the methods when configuring a UITableView.

The tableView:numberOfRowsInSection method is used to inform the table view how many rows are in the section and that returns the number of items in the “tableData” array.

The cellForRowAtIndexPath: method is called every time when a table row is displayed and this two method are the part UITableViewDataSource protocal.

Implement the methods for uitableview by adding the below code snippet into the ViewController class.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int     {        
return animalNames.count    
}  
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell      
{        
let cell: UITableViewCell = UITableViewCell(
style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")        
cell.textLabel.text = animalNames[indexPath.row]        
var image: UIImage = UIImage(named: animalImages[indexPath.row])!        
cell.imageView.image = image        
return cell    
}

Then, Build and Run the application the output will looks like below, and Download the UITableView tutorial in this link.

uitableview tutorial output

Filed Under: ios-tutorial, swift, uitableview

Comments

  1. Burak Demirpolat says

    January 31, 2016 at 11:02 pm

    Hi. I am teaching some code on sourcefreeze.com. Recent, I am doing example for UIWebView but stop the first step. I am only want to > when application starting, sourcefreze.com is open.But its not open. I have a problem. I am doing every steps until to first ” Build and Run”

    override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    let url = NSURL (string: “http://www.sourcefreeze.com”);

    let requestObj = NSURLRequest(URL: url!);

    myWebView.loadRequest(requestObj);

    its okey until these step

    but when i am click to “build and run”

    i have an error. ” App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file.”

    what is it mean? what am i doing theese error. Code is true, i am doing copy and paste from here, i have not any code error or red alert.

    Reply
  2. john says

    February 1, 2016 at 2:32 pm

    good example …..but please tell me how you can display code in your website???????

    Reply

Trackbacks

  1. UITableView tutorial using Swift in iOS | Dinesh Ram Kali. says:
    June 8, 2015 at 4:55 pm

    […] via UITableView tutorial using Swift in iOS – SourceFreeze. […]

    Reply

Leave a Reply Cancel reply

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

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS javascript mobile application development nextjs objective-c swift uiwebview Visual Studio plugin

Copyright © 2023 Source Freeze