从tableView到另一个ViewController的Swift数据

时间:2015-07-28 21:06:27

标签: swift tableview viewcontroller

我的第一次时间使用swift tableViews,我只是深入了解使用它们的基础知识。

目前在我的主故事板中有一个表,它有一个原型单元格。该原型单元具有“Disclosure Indicator”附件(箭头,链接到不同的View Controller。

每当我点击应用程序主页(tableView)中的某些内容时,成功会将我转移到另一个View Controller。

然而,我苦苦挣扎能够将表格中的信息转换为该屏幕。到目前为止,这是我的代码:

(基本假设低于代码)

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()
   }
}

假设我已经填写了州内每所大学的每个州名的所有初始变量,以及第二个字符串是每所大学的学生人口(如第一个例如,(“阿拉巴马大学”,“29843”)。

此外,假设“tableView”函数适用于所有变量(而不仅仅是“alabama”和“wisconsin”,就像现在一样)。

我想要发生什么是当我点击一所大学(例如“阿拉巴马大学”)时,当它重定向到新的视图控制器时,我希望它显示人口信息作为字符串(以及将来,我列出的每个学院的任何其他信息,如城市位置等)。

相信我需要使用一个新类并将其连接到我已经完成的View Controller。从这里开始,我对如何传递数据感到迷茫。

非常感谢!

2 个答案:

答案 0 :(得分:2)

正如您所说,一旦您使用segue将单元格连接到下一个视图控制器,您可以使用方法prepareForSegue:sender:,该函数通知视图控制器即将执行segue,它可以用于在segue连接的UIViewControllers之间传递数据,如下所示:

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
}

在上面的示例中,我假设您只有一个segue,如果您有多个segault,则需要使用属性检查器中的Interface Builder为每个要识别的segue设置标识符您已在Interface Builder中选择并修改上述代码,如下所示:

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
    }

}

我希望这对你有所帮助。

答案 1 :(得分:0)

您需要做什么:

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
        }
    }
}

首先,您将准备segue,并将发件人设置为已点按的UITableViewCell。通过从原型单元格拖动到下一个视图控制器,您将获得一个segue。执行此操作时,请确保在属性检查器中设置标识符。

然后我们将尝试创建一个视图控制器的新实例,此控件正在搜索。如果我们可以创建一个,通过检查destinationViewController是否是您期望的类型,那么我们可以设置目的地的属性,例如名称和人口,这些属性必须在目的地中设置控制器,如var schoolName = String()var schoolPopulation = Int()或您选择的任何类型的属性。

编辑:作为一个注释,如果我没有告诉你你的日期结构和设置很糟糕,我觉得我会错过这里的教学时刻,你应该考虑将它们移到模型课程中。