我想将数据从 UITableView 推送到 UIviewController。我想将 counter
设置为在 UITableView 中选择的行的值,然后将此值推送到 UIViewController。我遇到的问题是,最初无论我在 UITableView 中按哪一行,它都会设置 counter
= 0
然后,如果我关闭 ViewController 返回到 UITableView 并选择另一行,则 counter
的值将等于我选择的前一行。所以这将是一个压力。我该如何解决这个问题?
UITableViewController swift 文件是:
import Foundation
import UIKit
class TableViewController : UITableViewController {
@IBOutlet var Tableview: UITableView!
var sectionName = ["Day 0","Day 1","Day 2","Day 3","Day 4","Day 5","Day 6"]
var rowselected = Int()
override func viewDidLoad() {
super.viewDidLoad()
}
//setting up tableview rows
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionName.count
}
//what cell is & how it looks
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Tableview.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell!
cell?.textLabel?.text = sectionName[(indexPath as NSIndexPath).row]
cell?.textLabel?.textAlignment = .center
cell?.textLabel?.font = UIFont(name: "Avenir", size:30)
return cell!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! foodinfo
destinationVC.counter = rowselected
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
rowselected = indexPath.row
self.performSegue(withIdentifier: "dayx", sender: self)
}
我的 ViewController (foodinfo) swift 文件是:
import Foundation
import AVKit
class foodinfo: UIViewController {
var counter = Int()
@IBOutlet var tabs: UISegmentedControl!
@IBOutlet var shiftView: UIView!
@IBOutlet var theTitleLable: UILabel!
var simpleView1: UIView!
var simpleView2: UIView!
var theTitleArray = ["apple","orange","bananna","kiwi","grape","mango","avo"]
override func viewDidLoad() {
simpleView1 = SimpleVC1().view
simpleView2 = SimpleVC2().view
shiftView.addSubview(simpleView2)
shiftView.addSubview(simpleView1)
getTitle()
}
func getTitle() {
theTitleLable.text = theTitleArray[counter]
}
//segmented controller tabs for swapping old view
@IBAction func tabselected(_ sender: Any) {
switch (sender as AnyObject).selectedSegmentIndex {
case 0:
shiftView.bringSubviewToFront(simpleView1)
break
case 1:
shiftView.bringSubviewToFront(simpleView2)
break
case 2:
shiftView.bringSubviewToFront(simpleView1)
break
default:
break
}
}
//back to tableview
@IBAction func backToSections(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
最佳答案
您需要确保 segue 源是 viewController 本身而不是 cell ,就好像它是执行 segue 的单元格将在 didSelectRowAt
之前触发,因此计数器传递的值会产生误导
关于ios - 通过 segue 问题在 UITableView 和 UIViewController 之间推送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54963326/