UISegmentedControl with UITableView example in Swift. Part 1.

In this video tutorial I am going to share with you how to create a UISegmentedControl with three tabs which will allow user to switch between three different list of elements: Private, Protected and Public. The list of elements displayed in each tab is stored in an Array created within the same UIViewController.

If you would like to learn how to load list of items from a remote PHP script, watch this tutorial: UISegmentedControl with UITableView example in Swift. Part 2..

Source code: 

import UIKit

class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var mySegmentedControl: UISegmentedControl!
    @IBOutlet weak var myTableView: UITableView!
    
    let privateList:[String] = ["Private item 1","Private item 2"]
    let friendsAndFamily:[String] = ["Friend item 1","Friend item 2", "Friends item 3"]
    let publicList:[String] = ["Public item 1", "Public item 2", "Public item 3", "Public item 4"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
 
    }

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

  
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        var returnValue = 0
        
        switch(mySegmentedControl.selectedSegmentIndex)
        {
        case 0:
            returnValue = privateList.count
            break
        case 1:
            returnValue = friendsAndFamily.count
            break
            
        case 2:
            returnValue = publicList.count
            break
            
        default:
            break
            
        }
        
        return returnValue
        
    }
    
 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
        
        switch(mySegmentedControl.selectedSegmentIndex)
        {
        case 0:
            myCell.textLabel!.text = privateList[indexPath.row]
            break
        case 1:
            myCell.textLabel!.text = friendsAndFamily[indexPath.row]
            break
            
        case 2:
            myCell.textLabel!.text = publicList[indexPath.row]
            break
            
        default:
            break
            
        }


        return myCell
    }
    
 

    @IBAction func refreshButtonTapped(sender: AnyObject) {
        myTableView.reloadData()
    }
    
    @IBAction func segmentedControlActionChanged(sender: AnyObject) {
    
        myTableView.reloadData()
    }
    
 
}

  • Raed Alahmari

    Great tutorial. But I have problem second and third segment doesn’t load the arrays WHY?!!!!

    • Do you get any error messages?

      • Raed Alahmari

        No nothing!

        • Do you know if segmentedControlActionChanged function gets triggered? If you print(“function called”) into segmentedControlActionChanged function will it print something when you switch between segments?

          • Raed Alahmari

            Yup it dose.
            This is my code for “cellForRowAtIndexPath”
            http://i.imgur.com/KJI2pGo.png?1

          • Good. This tells me that segment control is working well… I would still check to make sure switch is working well by adding print statement inside of each switch case…. If it prints a value when you switch between segments then your table is also working… I think the issue is with business logic you have. I do not think it is correct to reload data inside of case:0 and case: 1. Looks like it might even cause an infinite loop. When table is being reloaded as soon as each cell is loaded again and again and again… Can you remove those reloadData()?

          • Raed Alahmari

            from the ActionChanged?!

          • Raed Alahmari

            I removed it from the ActionChanged but it load the same what ever on case 0 for all the cases?!

          • Raed Alahmari

            Yup it dose.
            This is my code for “cellForRowAtIndexPath”
            http://i.imgur.com/KJI2pGo.png?1