我想将数据从UITableView推送到UIviewController。我想将counter
设置为在UITableView中选择的行的值,然后将此值推入UIViewController中。我遇到的问题是,最初,无论我在UITableView中按下哪一行,它都会设置counter
= 0
然后,如果我解雇了ViewController返回UITableView并选择另一行,则counter
的值将等于我选择的上一行。因此将成为新闻的背后。我该如何解决?
UITableViewController快速文件是:
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)快捷文件是:
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)
}
}
答案 0 :(得分:1)
您需要确保segue的源是viewController本身而不是cell,就好像segue执行的单元是didSelectRowAt
之前被触发的那个单元一样,因此计数器传递的值会误导
答案 1 :(得分:0)
您创建每次计数器。不要这样做。您尝试;
class foodinfo: UIViewController {
var counter:Int = 0
}