Swift - 2个tableviews和detailviewcontroller

时间:2015-06-09 20:41:22

标签: swift

在2个viewcontrollers和detailviewcontroller上有2个tableviews的问题,一切都工作正常,2个tableviews像魅力一样工作,但问题是在secondviewcontroller中我在每次用餐/点击食物后把图像数组转换为detailVC,我有墨西哥食物,西班牙食物,意大利语,克罗地亚语和法语3相同的图像。图像(15)确实对应于15餐。在第二个视图控制器中放置图像数组我出错了吗?谢谢

1 2 3

的ViewController

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


}
}

SecondViewController

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

}
}

DetailViewController(显示15张15张图片)

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var backgroundImage: UIImageView!

@IBOutlet weak var slika: UIImageView!

var picturesToRetrieve:String!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    var connection = self.picturesToRetrieve
    self.slika.image = UIImage(named: connection)

}

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

2 个答案:

答案 0 :(得分:1)

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

但是你几乎完成了更简单的解决方案是将值从第一个表传递到第二个表,所以假设用户选择了你传递的第2行并保留了这些值。

现在在第二个表格中,如果用户选择第3行,则将用户选择的行数(3)乘以表格视图中的第一行选择数(1),您知道必须显示最终阵列中的图片编号为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)
}

这是基本的想法,抱歉我现在无法测试它,因为我现在在Windows计算机上,但应该正常工作

答案 1 :(得分:1)

在View Controller中存储选定的索引并将其传递给DrugiViewController

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

就是这样