Read This Offline With Code With Me APP

Populate UITableView from internet using Alomofire Swift

Posted On: Jul 09, 2016

Hello, Today i am going to show how easy it is to use networking in IOS swift. In this tutorial I am using Alomofire network library. I assume you have cocoapods installed
Create new Xcode project

Screen Shot 2016-07-09 at 3.03.03 AM  
Now create new pod file, i use terminal

Open terminal and go to the location of your project and type
pod init

Now open "Podfile" in Xcode and type following

here my project is Nwtworking, I changed the name to be bit odd. its my typo lol

platform :ios, '9.0'


target 'Nwtworking' do
pod 'Alamofire', '~> 3.1.2'
pod 'SwiftyJSON'
pod 'AlamofireImage', '~> 2.0'

Open Terminal and type following to install dependancy

pod install

and it will install all required dependencies

Now open Main.storyboard and add UITableView and one UITableViewCell with identifier "cell"
Screen Shot 2016-07-09 at 2.53.15 AM

Create new Swift class StaffTableViewCell.swift

import UIKit

class StaffTableViewCell: UITableViewCell {
    @IBOutlet weak var imgStaff: UIImageView!
    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var lblPost: UILabel!

    override func awakeFromNib() {
        // Initialization code

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state


Now set StaffTableViewCell as the class name in identity Inspector of Cell created in storyboard. Staff create IBOutlet for controls in cell,

Create new Swift class for Model: lets say Staff.swift

import Foundation
public class Staff{
    private var name: String?
    private var image: String?
    private var post: String?
    public func name(name: String) -> Staff {
        return self
    public func image(image: String) -> Staff {
        return self
    public func post(post: String) -> Staff {
        return self
    public func getName() -> String {
    public func getImage() -> String {
        return self.image!
    public func getPost() -> String {
    public  init(){

Now add following code to ViewController.swift

import UIKit
import Alamofire
import AlamofireImage
import SwiftyJSON

class ViewController: UIViewController {
    var names:[Staff]=[]
    @IBOutlet weak var tableViewStaff: UITableView!
    var request: HttpRequest?
    var messageFrame = UIView()
    var activityIndicator = UIActivityIndicatorView()
    var strLabel = UILabel()
    override func viewDidLoad() {
        self.tableViewStaff.delegate = self
        self.tableViewStaff.dataSource = self
        //show activity indicator
        progressBarDisplayer("Loading Data",true)

        Alamofire.request(.POST, "", parameters: ["level": "Secondary"])
            .response { request, response, data, error in
                if let err=error {
                    let json=JSON(data:data)
                    for (ind,staff) in json {
    func progressBarDisplayer(msg:String, _ indicator:Bool ) {
        strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
        strLabel.text = msg
        strLabel.textColor = UIColor.whiteColor()
        messageFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
        messageFrame.layer.cornerRadius = 15
        messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
        if indicator {
            activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
            activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.


extension ViewController:UITableViewDelegate, UITableViewDataSource{
    internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.names.count
    internal func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: StaffTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! StaffTableViewCell
        let staff = names[indexPath.row]
        cell.lblName.text = staff.getName()
        cell.lblPost.text = staff.getPost()
        Alamofire.request(.GET, staff.getImage())
            .responseImage() { response in
                if let image = response.result.value{
                    cell.imgStaff.image = image
        return cell


  • alomofire
  • ios
  • swift
  • uitableview

Share This On


© Copyright 2014,