在2個viewcontrollers和detailviewcontroller上有兩個tableviews有問題,一切幾乎都可以正常工作,2個tableviews像一個魅力一樣工作,但問題是在secondviewcontroller中,我把圖像數組放到detailVC之後餐點/食物點擊,我得到了墨西哥食物,西班牙食物,意大利,克羅地亞和法國相同的3張圖片。圖像(15)確實對應於15餐。我在第二個視圖控制器中放置圖像陣列時出錯了嗎?謝謝Swift - 2 tableviews和detailviewcontroller
的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.
}
}
我迷惑,你的應用程序有5個國家,表視圖1通第二視圖控制器1和5之間的指數,第二視圖控制器始終有3個盤子,因此它總是將1和3之間的值傳遞給視圖控制器。所以你在第二個視圖控制器中唯一能看到的就是前三個盤子 – Icaro
對,我有15個食物對應5個國家,意大利有3個,墨西哥有自己的3個,西班牙有自己的3個等等,以及如何顯示每個食物的圖像,我有15個圖像,只顯示3。因此,當我點擊墨西哥食物,然後點擊玉米餅,如何顯示tortilja圖片,它只顯示索引0,1,2的圖像陣列,如何顯示15個圖像到每個食物,ty – Igy
我把一個解釋請告訴我如何爲您的實施實施修復措施,以及如何解決問題。我希望你能夠明白。 – Icaro