gpt4 book ai didi

ios - Swift- 将数据从多节 TableView 传递到 ViewController

转载 作者:行者123 更新时间:2023-11-29 12:16:53 24 4
gpt4 key购买 nike

我有一个两部分的表格 View (见下图)

enter image description here

当我点击“A”行时,会出现另一个带有字母“A”的表格 View

enter image description here

当我点击“B”行时,会出现另一个带有字母“B”的表格 View

enter image description here

单击 C 行时如何将字母“C”传递给另一个表格 View ?

这是我在 TableView 中的代码:导入 UIKit

类 manhinh1:UITableViewController {

var UserDefault:NSUserDefaults!
var array:[[String]]!
override func viewDidLoad() {
super.viewDidLoad()

UserDefault = NSUserDefaults()
array = [["A","B"],["C","D","E"]]

}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return array.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if section == 0 {
return array[0].count
}
if section == 1 {
return array[1].count
}
return 1
}


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

if indexPath.section == 0 {
cell.textLabel?.text = array[0][indexPath.row]
}
if indexPath.section == 1 {
cell.textLabel?.text = array[1][indexPath.row]
}
// Configure the cell...

return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var row = self.tableView.indexPathForSelectedRow()!.row

UserDefault.setObject(array[0][row], forKey: "selected")
}

这是第二个 View 中的代码:导入 UIKit

类 View Controller :UIViewController {

var UserDefault:NSUserDefaults!
@IBOutlet weak var lbldata: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
UserDefault = NSUserDefaults()
// Do any additional setup after loading the view, typically from a }
lbldata.text = UserDefault.objectForKey("selected") as! String
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

我确实在 prepareForSegue 中尝试了一些代码,但没有成功。我希望你们能告诉我一个方法。谢谢

最佳答案

您必须获取行和部分才能访问您的多维数组。然后获取 segue 的目标 View Controller 并将数组中的相应值分配给 UserDefault 属性:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var row = self.tableView.indexPathForSelectedRow()!.row
var section = self.tableView.indexPathForSelectedRow()!.section

var destinationViewController = segue.destinationViewController as! ViewController

destinationViewController.UserDefault.setObject(array[section][row], forKey: "selected")
}

关于ios - Swift- 将数据从多节 TableView 传递到 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31789407/

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