[source code] iOS Development with Swift Tutorial - 20, 21, & 22 - Table Views

+1 Bucky Roberts · February 2, 2015
ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

    let people = [
        ("Bucky Roberts", "New York"),
        ("Lisa Tucker", "Alabama"),
        ("Emma Hotpocket", "Texas")
    ]
    
    let videos = [
        ("Android App Development", "74 videos"),
        ("C++ for Beginners", "87 videos"),
        ("Java", "142 videos"),
        ("Python Programming", "63 videos"),
        ("Web Design", "68 videos")
    ]
    
    //how many sections are in your table
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
    //return Int, how many rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            return people.count
        }else{
            return videos.count
        }
        
    }
    
    //what are the contents of each cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        
        if indexPath.section == 0{
            var (personName, personLocation) = people[indexPath.row]
            cell.textLabel?.text = personName
        }else{
            var (videoTitle, videoDesc) = videos[indexPath.row]
            cell.textLabel?.text = videoTitle
        }
        
        return cell
    }
    
    //Give each table section a title
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "People"
        }else{
            return "Videos"
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Vinh Thomas · September 10, 2015
Hello Bucky,

I' m looking for code in swift to loop through the table view cell that have 3 text boxes in a row.  These rows are dynamically created and on click of a button, I need to perform a loop and print out the values on each row or do something with it.

Can you help please.

Here is my email address:  vinh@aroma.com.

Thank you,
Vinh 
  • 1

iOS Development / Swift

107,088 followers
About

This forum is for questions, comments, and general discussion about iOS Development and Swift.

Links
Moderators
Bucky Roberts Administrator