Swift-2 TableView和detailviewcontroller

Swift-2 TableView和detailviewcontroller,swift,Swift,在2个ViewController和detailviewcontroller上有2个TableView的问题,一切都很正常,2个TableView工作得很好,但问题是在secondviewcontroller中,我在每顿饭/食物被点击后将一组图像切换到detailVC,我得到了同样的3张墨西哥食物、西班牙食物、意大利食物的图像,克罗地亚语和法语。图像(15)与15顿饭对应。我在第二视图控制器中放置图像数组时出错了吗?多谢各位 视图控制器 import UIKit class ViewCon

在2个ViewController和detailviewcontroller上有2个TableView的问题,一切都很正常,2个TableView工作得很好,但问题是在secondviewcontroller中,我在每顿饭/食物被点击后将一组图像切换到detailVC,我得到了同样的3张墨西哥食物、西班牙食物、意大利食物的图像,克罗地亚语和法语。图像(15)与15顿饭对应。我在第二视图控制器中放置图像数组时出错了吗?多谢各位

视图控制器

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

let textForFirstTableView = ["Italian food", "Mexican food", "Croatian food", "Spanish food", "French food"]

let namesOfFood = [["Bolognese", "Milagnese","Pizza"],
                   ["Tortilla", "Chimichanga", "Paella"],
                   ["Burek od mesa","Grah", "Janjetina"],
                   ["Tapas", "Churros", "Flan"],
                   ["Buche de Noel", "Cherry Cake", "Onion Soup"]]

var ObjectNamesOfFood = [String]()

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    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 textForFirstTableView.count

}

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

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]

    self.performSegueWithIdentifier("Segue", sender: self)

}

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

    let driver = segue.destinationViewController as! DrugiViewController

    var whatToPass = self.ObjectNamesOfFood

    driver.arrayToPass = whatToPass


}
}
import UIKit

class DrugiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

var arrayToPass:[String] = [String]()

var picturesForEachMeal = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]

var objectpicturesForEachMeal:String!


@IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.tableView2.delegate = self
    self.tableView2.dataSource = self
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrayToPass.count
}

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

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row]

     self.performSegueWithIdentifier("toDetailViewController", sender: self)

}

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

    let driver = segue.destinationViewController as! DetailViewController

    driver.picturesToRetrieve = objectpicturesForEachMeal

}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.selectedKitchen = indexPath.row 
    self.performSegueWithIdentifier("Segue", sender: self)
}
第二视图控制器

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

let textForFirstTableView = ["Italian food", "Mexican food", "Croatian food", "Spanish food", "French food"]

let namesOfFood = [["Bolognese", "Milagnese","Pizza"],
                   ["Tortilla", "Chimichanga", "Paella"],
                   ["Burek od mesa","Grah", "Janjetina"],
                   ["Tapas", "Churros", "Flan"],
                   ["Buche de Noel", "Cherry Cake", "Onion Soup"]]

var ObjectNamesOfFood = [String]()

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    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 textForFirstTableView.count

}

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

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]

    self.performSegueWithIdentifier("Segue", sender: self)

}

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

    let driver = segue.destinationViewController as! DrugiViewController

    var whatToPass = self.ObjectNamesOfFood

    driver.arrayToPass = whatToPass


}
}
import UIKit

class DrugiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

var arrayToPass:[String] = [String]()

var picturesForEachMeal = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]

var objectpicturesForEachMeal:String!


@IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.tableView2.delegate = self
    self.tableView2.dataSource = self
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrayToPass.count
}

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

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row]

     self.performSegueWithIdentifier("toDetailViewController", sender: self)

}

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

    let driver = segue.destinationViewController as! DetailViewController

    driver.picturesToRetrieve = objectpicturesForEachMeal

}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.selectedKitchen = indexPath.row 
    self.performSegueWithIdentifier("Segue", sender: self)
}
DetailViewController(为15顿饭显示15幅图像)


我必须说,如果您从一开始就启动应用程序,那么这不是最好的方法。在您的情况下,您应该使用包含关键国家/地区的数组字典,从而在第一视图控制器中使用字典盘和照片数组。 在第二个视图控制器中,您应该有一个包含碟片和照片的字典数组。 在第三个视图中,只是一个简单的字典,其中包含一个带有dish和photo的字典

但是,对于您来说,如何完成更简单的解决方案是将值从第一个表传递到第二个表,因此假设用户选择了第2行,您将传递并保留该值

现在在第二个表中,如果用户选择了第3行,则将用户选择的行数(3)乘以表视图1(2)中选择的第一行数,您知道必须在最终数组中显示图片编号6

我希望这对你有帮助,我相信现在你知道了这个想法,你将能够完成你的项目。祝你好运

第一视图控制器:

var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.rowSelected = indexPath.row
    self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let driver = segue.destinationViewController as! DrugiViewController
    var whatToPass = self.ObjectNamesOfFood
    driver.arrayToPass = whatToPass
    driver.rowSelected = rowSelected
}
var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row * self.rowSelected]
     self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
第二视图控制器:

var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.rowSelected = indexPath.row
    self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let driver = segue.destinationViewController as! DrugiViewController
    var whatToPass = self.ObjectNamesOfFood
    driver.arrayToPass = whatToPass
    driver.rowSelected = rowSelected
}
var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row * self.rowSelected]
     self.performSegueWithIdentifier("toDetailViewController", sender: self)
}

这是基本的想法,很抱歉,我无法测试它,因为我现在在Windows计算机中,但应该只在视图控制器中工作,存储选定的索引并将其传递给DrugiViewController

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

let textForFirstTableView = ["Italian food", "Mexican food", "Croatian food", "Spanish food", "French food"]

let namesOfFood = [["Bolognese", "Milagnese","Pizza"],
                   ["Tortilla", "Chimichanga", "Paella"],
                   ["Burek od mesa","Grah", "Janjetina"],
                   ["Tapas", "Churros", "Flan"],
                   ["Buche de Noel", "Cherry Cake", "Onion Soup"]]

var ObjectNamesOfFood = [String]()

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    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 textForFirstTableView.count

}

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

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]

    self.performSegueWithIdentifier("Segue", sender: self)

}

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

    let driver = segue.destinationViewController as! DrugiViewController

    var whatToPass = self.ObjectNamesOfFood

    driver.arrayToPass = whatToPass


}
}
import UIKit

class DrugiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

var arrayToPass:[String] = [String]()

var picturesForEachMeal = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]

var objectpicturesForEachMeal:String!


@IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.tableView2.delegate = self
    self.tableView2.dataSource = self
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrayToPass.count
}

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

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row]

     self.performSegueWithIdentifier("toDetailViewController", sender: self)

}

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

    let driver = segue.destinationViewController as! DetailViewController

    driver.picturesToRetrieve = objectpicturesForEachMeal

}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.selectedKitchen = indexPath.row 
    self.performSegueWithIdentifier("Segue", sender: self)
}
在DrugiViewController中,计算每个食品图像索引

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     var totalIndex = (indexPath.row) + (selectedKitchen * 3)
     self.objectpicturesForEachMeal = self.picturesForEachMeal[totalIndex]
     self.performSegueWithIdentifier("toDetailViewController", sender: self)
}

就是这样

我很困惑,你的应用程序有5个国家,table view 1将1到5之间的索引传递给second view controller,second view controller始终有3个盘子,因此它将始终将1到3之间的值传递给view controller。因此,在第二个视图控制器中,您可以看到的唯一内容是前3道菜是的,我有15道菜,分别对应于5个国家,意大利人有3道菜,墨西哥人有3道菜,西班牙人有3道菜,依此类推,以及如何显示每道菜的图像,我有15道图像,只显示了3道菜。因此,当我点击墨西哥食物,然后点击玉米饼,如何显示玉米饼图片,它只显示图像数组的索引0,1,2,如何为每种食物显示15个图像,我在下面解释了如何为您的实现实现修复,以及我将如何处理该问题。我希望你能理解。非常感谢,但你的简单答案有点难理解,我是swift的初学者,我已经对这个应用程序感到困惑,再次感谢thx的帮助,所以如果我将图像数组移动到first view controller,那么我还遇到了一个问题,如何在名称数组中“合并”索引0-15到索引0-5,是的,问题,…忘记第一部分,实现第二部分,将行号从vc1传递到vc2,将你传递的行号乘以在vc2中选择的新行号,然后传递到你的视图,现在你有了正确的值抱歉打扰你,我在谷歌上搜索了如何在swift中传递行号,但找不到任何相关信息,我以前从来没有这样做过,尤其是乘行。我成功地实现了你的代码,但现在它在不同的餐点上显示不同的图片,就像图片的索引与餐点的索引不匹配一样。你需要弄清楚数组中的顺序,以便现在显示正确的图片,只要根据需要进行更改即可