【发布时间】: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”,就像现在这样)。
我希望发生的事情是当我点击一所大学(例如“阿拉巴马大学”)时,当它重定向到新的视图控制器时,我希望它显示人口字符串形式的信息(以及将来我在每个学院中列出的任何其他信息,例如城市位置等...)。
我相信我需要使用一个新类并将其连接到我的视图控制器,我已经这样做了。从这里开始,我不知道如何传递数据。
非常感谢!
【问题讨论】:
标签: swift tableview viewcontroller