Swift 3 tableviews with detailVC

时间:2015-11-16 23:09:10

标签: ios swift uitableview

我练习结构和表格,直到卡住才很有趣。我在第一个桌面视图中有4家汽车公司,(奥迪,宝马,梅赛德斯,欧宝)然后点击一家汽车公司,我可以选择多年,2015年,2014年等等,每年应该有一套不同的汽车(制造)在那一年),但我每年都得到相同的汽车(audi1,audi2,audi3,audi4),当点击不同的公司时,我为每家公司分别获得了多年的工作。那么,在哪里放置阵列以及2014年如何展示audi5,audi6,audi7,AUDI8。当点击汽车时,它应该显示该汽车的详细文本和图像,这一切都在没有tableview的情况下工作多年,我设法用2个tableviews与detailvc一起工作,但现在尝试额外的tableview,(年)和得到卡住。

FirstTableViewcontroller

Import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var firstArray = [String]()
var secondArray = [FirstStruct]()
var thirdArray = [SecondStruct]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    firstArray = ["Audi", "Bmw", "Mercedes", "Opel"]

    secondArray = [FirstStruct(secondTitle: ["2015", "2014"]),
        FirstStruct(secondTitle: ["2015", "2014"]),
        FirstStruct(secondTitle: ["2015", "2014"]),
        FirstStruct(secondTitle: ["2015", "2014"])]

    thirdArray = [SecondStruct(thirdTitle: ["Audi 1", "Audi 2", "Audi 3", "Audi 4"]),
        SecondStruct(thirdTitle: ["Bmw1", "Bmw2", "Bmw3", "Bmw4"]),
        SecondStruct(thirdTitle: ["Mercedes1", "Mercedes2", "Mercedes3", "Mercedes4"]),
        SecondStruct(thirdTitle: ["Opel 1", "Opel 2", "Opel 3", "Opel 4"])]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return firstArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel?.text = firstArray[indexPath.row]
    return cell

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let destVC = segue.destinationViewController as! SecondTableViewController

    let join : FirstStruct
    join = secondArray[indexPath.row]

    destVC.firstArray = join.secondTitle

    let join2 : SecondStruct
    join2 = thirdArray[indexPath.row]

    destVC.secondArray = join2.thirdTitle


}
}

Secondtableviewcontroller

import UIKit

class SecondTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!

var firstArray = [String]()
var secondArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return firstArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = firstArray[indexPath.row]

    return cell

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let destVC = segue.destinationViewController as! ThirdTableViewController

    //let divide = secondArray[indexPath.row]
    destVC.firstArray = secondArray


}
}
}

ThirdTableViewController

import UIKit

class ThirdTableViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var firstArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return firstArray.count

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = firstArray[indexPath.row]

    return cell
}
}

0 个答案:

没有答案