gpt4 book ai didi

Swift - 在两个 TableView (2 个 View Controller )之间传递数据

转载 作者:可可西里 更新时间:2023-11-01 01:40:00 25 4
gpt4 key购买 nike

表格 View 有问题,它只是 View Controller 上的表格 View ,我在第一个表格 View 中有意大利菜、墨西哥菜、英国菜,无论我点击什么,我到处都是意大利菜“肉酱、米拉涅塞、比萨饼”,用墨西哥语和英语食物。为什么当我点击意大利菜时,我在第二个 tableview 中看不到前两行意大利菜?我还没有为该行输入墨西哥食物和英国食物数组,因为我只是测试意大利食物数组。第二个 TableView 如何知道第一个 TableView 已被单击?使用 didselectrowAtIndexPath 方法对我来说很重要。非常感谢!

firsttableview


secondtableview

代码如下:

ViewController (firsttableview)

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
let namesForCell = ["Italian Cousine", "Mexican Food", "English kitchen"]
let textWhenRowIsClicked = ["Bolognese", "Milagnese", "Ravioli"]


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

}

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

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

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

return cell

}

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


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

}

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

let detailVC = segue.destinationViewController as! secondTableView

var whatToPass = self.textWhenRowIsClicked

detailVC.namesForCell2 = whatToPass



}

Secondtableview (viewcontroller)

import UIKit

class secondTableView: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView1: UITableView!

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

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
self.tableView1.delegate = self
self.tableView1.dataSource = self

}

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

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

return namesForCell2.count
}

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

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

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

return cell

}

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

//self.talijanskaJela = self.tekstKadSeKlikneRow[indexPath.row] as String

// self.performSegueWithIdentifier("toTableView", sender: self)

}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

最佳答案

您可以在屏幕截图中看到所有数据都在那里,问题是您的第二个 TableView 位于导航栏下方,您可以通过在第二个 View Controller 的 viewDidLoad 中添加以下命令来修复它。这将在表格 View 的顶部添加一些空间,因此将在导航栏完成时开始:

table1.ScrollIndicatorInsets = new UIEdgeInsets(64, 0, 0, 0);

对于食物列表,您总是将相同的食物从一张 table 传递到另一张 table ,在示例中我没有在数组中添加食物名称,请在测试之前这样做:

let namesForCell = ["Italian Cousine", "Mexican Food", "English kitchen"]
let textWhenRowIsClicked = [["Bolognese", "Milagnese", "Ravioli"],[mexican array], [Englis Array]] //bidimentional array to keep all foods from all kitchens in one array
var listOfFoodToPass = [String]() //Keep the array to be pass

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.listOfFoodToPass = textWhenRowIsClicked[indexPath.row] //put the correct list of the food in the array to be passed
self.performSegueWithIdentifier("toTableView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let detailVC = segue.destinationViewController as! secondTableView
var whatToPass = self.listOfFoodToPass //pass the array to the new table with the correct list of food
detailVC.namesForCell2 = whatToPass
}

希望对你有帮助!

关于Swift - 在两个 TableView (2 个 View Controller )之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30721474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com