Swift 数据从 tableView 到不同的 ViewController
Posted
技术标签:
【中文标题】Swift 数据从 tableView 到不同的 ViewController【英文标题】:Swift Data from tableView to a different ViewController 【发布时间】:2015-07-28 21:06:27 【问题描述】:这是我第一次第一次使用 swift tableViews,我刚刚开始学习使用它们的基础知识。
我目前在我的主情节提要中有一个表格,其中有一个原型单元格。这个原型单元有一个“Disclosure Indicator”附件(一个箭头,它链接到不同的视图控制器。
每当我从我的应用程序的主页(tableView)点击某个东西时,它成功将我转移到另一个视图控制器。
但是,我努力能够将表格中的信息转换到该屏幕上。到目前为止,这是我的代码:
(基本假设在代码下方)
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”,就像现在这样)。
我希望发生的事情是当我点击一所大学(例如“阿拉巴马大学”)时,当它重定向到新的视图控制器时,我希望它显示人口字符串形式的信息(以及将来我在每个学院中列出的任何其他信息,例如城市位置等...)。
我相信我需要使用一个新类并将其连接到我的视图控制器,我已经这样做了。从这里开始,我不知道如何传递数据。
非常感谢!
【问题讨论】:
【参考方案1】:正如您所说,一旦您使用 segue 将单元连接到下一个视图控制器,您可以使用方法 prepareForSegue:sender:
通知视图控制器即将执行 segue 并且可以使用在由 segues 连接的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,如果你有多个 segue,你需要在 Attributes Inspector 中使用 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
希望对你有所帮助。
【讨论】:
这就是我现在所在的位置:override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) if segue.identifier == "College" let nextViewController = segue.destinationViewController as NextViewController nextViewController.schoolName = sender.textLabel??.text
这可以正确地将大学名称放入 schoolName 变量中。但是,人口数被忽略了。如何将其访问到不同的变量中?【参考方案2】:
你需要做什么:
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()
或您选择的任何类型的属性。
编辑:作为说明,如果我没有告诉你你的日期结构和设置很糟糕,我觉得我会错过这里的教学时间,你应该考虑将它们转移到模型类中。
【讨论】:
以上是关于Swift 数据从 tableView 到不同的 ViewController的主要内容,如果未能解决你的问题,请参考以下文章
SWIFT:从定义 tableView 到 tableViewCell 的 ViewController 通信数据
将数据从 TableView 发送到 DetailView Swift
Swift Tableview 从 Firebase 数据库加载数据
Swift:将数据从 tableView 显示到另一个 ViewController(JSON、Alamorife、AlamofireImage)