Reputation: 1212
It is my first time working with swift tableViews, and I'm just getting into the basics of using them.
I currently have a table in my main storyboard, that has a prototype cell. This prototype cell has a "Disclosure Indicator" Accessory (an arrow, which links to a different View Controller.
Whenever I click on something from the home page of my application (the tableView), it successfully transfers me to the other View Controller.
However, I'm struggling to be able to convert information from the table onto that screen. Here is my code so far:
(Basic assumptions are below the code)
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var alabama = [
("University of Alabama", "29843")
]
var arizona = [
]
var arkansas = [
]
var california = [
]
var colorado = [
]
var connecticut = [
]
var districtofcolumbia = [
]
var florida = [
]
var georgia = [
]
var hawaii = [
]
var illinois = [
]
var indiana = [
]
var iowa = [
]
var kansas = [
]
var kentucky = [
]
var louisiana = [
]
var maine = [
]
var maryland = [
]
var massachusetts = [
]
var michigan = [
]
var minnesota = [
]
var mississippi = [
]
var missouri = [
]
var nebraska = [
]
var nevada = [
]
var newhampshire = [
]
var newjersey = [
]
var newmexico = [
]
var newyork = [
]
var northcarolina = [
]
var ohio = [
]
var oklahoma = [
]
var oregon = [
]
var pennsylvania = [
]
var puertorico = [
]
var rhodeisland = [
]
var southcarolina = [
]
var tennessee = [
]
var texas = [
]
var utah = [
]
var vermont = [
]
var virginia = [
]
var washington = [
]
var westvirginia = [
]
var wisconsin = [
("University of Wisconsin", "12345")
]
//How many sections are in your table?
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 45
}
//How many rows are in your table?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return alabama.count
}
if section == 1 {
return arizona.count
}
if section == 2 {
return arkansas.count
}
if section == 3 {
return california.count
}
if section == 4 {
return colorado.count
}
if section == 5 {
return connecticut.count
}
if section == 6 {
return districtofcolumbia.count
}
if section == 7 {
return florida.count
}
if section == 8 {
return georgia.count
}
if section == 9 {
return hawaii.count
}
if section == 10 {
return illinois.count
}
if section == 11 {
return indiana.count
}
if section == 12 {
return iowa.count
}
if section == 13 {
return kansas.count
}
if section == 14 {
return kentucky.count
}
if section == 15 {
return louisiana.count
}
if section == 16 {
return maine.count
}
if section == 17 {
return maryland.count
}
if section == 18 {
return massachusetts.count
}
if section == 19 {
return michigan.count
}
if section == 20 {
return minnesota.count
}
if section == 21 {
return mississippi.count
}
if section == 22 {
return missouri.count
}
if section == 23 {
return nebraska.count
}
if section == 24 {
return nevada.count
}
if section == 25 {
return newhampshire.count
}
if section == 26 {
return newjersey.count
}
if section == 27 {
return newmexico.count
}
if section == 28 {
return newyork.count
}
if section == 29 {
return northcarolina.count
}
if section == 30 {
return ohio.count
}
if section == 31 {
return oklahoma.count
}
if section == 32 {
return oregon.count
}
if section == 33 {
return pennsylvania.count
}
if section == 34 {
return puertorico.count
}
if section == 35 {
return rhodeisland.count
}
if section == 36 {
return southcarolina.count
}
if section == 37 {
return tennessee.count
}
if section == 38 {
return texas.count
}
if section == 39 {
return utah.count
}
if section == 40 {
return vermont.count
}
if section == 41 {
return virginia.count
}
if section == 42 {
return washington.count
}
if section == 43 {
return westvirginia.count
}
if section == 44 {
return wisconsin.count
}
else
{
return 0
}
}
//What are the contents of each cell?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//var cell = UITableViewCell()
let cell = tableView.dequeueReusableCellWithIdentifier("College Cell", forIndexPath: indexPath) as UITableViewCell
if indexPath.section == 0{
var (collegeName, population) = alabama[indexPath.row]
cell.textLabel?.text = "\(collegeName)"
}
else
{
var (collegeName, population) = wisconsin[indexPath.row]
cell.textLabel?.text = "\(collegeName)"
}
return cell
}
//Give each table section a title
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "alabama"
}
if section == 1 {
return "arizona"
}
if section == 2 {
return "arkansas"
}
if section == 3 {
return "california"
}
if section == 4 {
return "colorado"
}
if section == 5 {
return "connecticut"
}
if section == 6 {
return "district of columbia"
}
if section == 7 {
return "florida"
}
if section == 8 {
return "georgia"
}
if section == 9 {
return "hawaii"
}
if section == 10 {
return "illinois"
}
if section == 11 {
return "indiana"
}
if section == 12 {
return "iowa"
}
if section == 13 {
return "kansas"
}
if section == 14 {
return "kentucky"
}
if section == 15 {
return "louisiana"
}
if section == 16 {
return "maine"
}
if section == 17 {
return "maryland"
}
if section == 18 {
return "massachusetts"
}
if section == 19 {
return "michigan"
}
if section == 20 {
return "minnesota"
}
if section == 21 {
return "mississippi"
}
if section == 22 {
return "missouri"
}
if section == 23 {
return "nebraska"
}
if section == 24 {
return "nevada"
}
if section == 25 {
return "new hampshire"
}
if section == 26 {
return "new jersey"
}
if section == 27 {
return "new mexico"
}
if section == 28 {
return "new york"
}
if section == 29 {
return "north carolina"
}
if section == 30 {
return "ohio"
}
if section == 31 {
return "oklahoma"
}
if section == 32 {
return "oregon"
}
if section == 33 {
return "pennsylvania"
}
if section == 34 {
return "puerto rico"
}
if section == 35 {
return "rhode island"
}
if section == 36 {
return "south carolina"
}
if section == 37 {
return "tennessee"
}
if section == 38 {
return "texas"
}
if section == 39 {
return "utah"
}
if section == 40 {
return "vermont"
}
if section == 41 {
return "virginia"
}
if section == 42 {
return "washington"
}
if section == 43 {
return "west virginia"
}
if section == 44 {
return "wisconsin"
}
else
{
return ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Assume that I have filled in all of the initial variables of each state name with each college in the state, as well as the second string being the student population of each college (as in the first example, ("University of Alabama", "29843").
Furthermore, assume that the "tableView" function appropriately works for all variables (and not just "alabama" and "wisconsin", as it is now).
What I would like to happen is when I click on a college (such as "University of Alabama"), when it redirects to the new View Controller, I would like it to display the population information as a string (and in the future, any other information I list with each college, such as City Location, etc...).
I believe that I need to use a new class and connect it to my View Controller, which I have done. From here, I am lost as to how to the pass the data across.
Thank you very much!
Upvotes: 0
Views: 128
Reputation: 23459
As you said, once you're connected your cell to the next view controller using a segue you can use the method prepareForSegue:sender:
which function is notifies the view controller that a segue is about to be performed and it could be used to pass data between UIViewControllers
connected by segues, like in the following way:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Here you use the segue.destinationViewController to access to the next view controller
let nextViewController = segue.destinationViewController as! NextViewControllerName
// here you can access to the properties of the class instantiated and set it data
// nextViewController.propertyName = value
}
In the above example I assume you only have one segue, in case you have more than one you need to set the identifier for each segue you want to identify using Interface Builder in the Attributes Inspector once you have selected in Interface Builder and the modify the above code like in the following way:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "nameYouSetForYourSegue" {
// Here you use the segue.destinationViewController to access to the next view controller
let nextViewController = segue.destinationViewController as! NextViewControllerName
// here you can access to the properties of the class instantiated and set it data
// nextViewController.propertyName = value
}
}
I hope this help you.
Upvotes: 2
Reputation: 5248
What you'll need to do:
override func prepareForSegue(segue: UIStoryboardSegue, sender: UITableViewCell) {
if segue.identifier == "yourStoryboardSegue" { //you'll set this up by control dragging to the next view controller
if let destinationVC = segue.destinationViewController as? YourViewControllerClass {
destinationVC.schoolName = sender.textLabel?.text
destinationVC.schoolPopulation = sender.detailTextLabel?.text
}
}
}
In order, first you'll prepare for segue, and set the sender as the UITableViewCell
that was tapped. By dragging from your prototype cell to your next view controller, you'll then have a segue. Make sure to set the identifier in the attributes inspector when you do this.
Then we'll try and create a new instance of the view controller this tap is seguing to. If we can create one, by checking to see if the destinationViewController
is of the type you're expecting, then we can set properties of the destination, such as the name and population, which have to be set in the destination controller like var schoolName = String()
and var schoolPopulation = Int()
or whatever types of properties you choose.
Edit: As a note, I feel I would miss out on a teaching moment here if I didn't tell you that your date structure and setup are horrible, and you should look into moving them into a model class.
Upvotes: 0