[英]Why is my variable is changing in printLn but not in the tableview Swift
I've spent ages trying to solve this but with no resolve. 我花了很长时间试图解决这个问题,但是没有解决的办法。
I've finally got to a point where I am pulling my data from one one controller and moving it to the destination controller when I unwind segue, however, when the variable is only reloading in the println
but not in the tableview. 我终于到了要点,当我解散segue时,我将数据从一个控制器中拉出并将其移动到目标控制器,但是,当变量仅在
println
而不是在tableview中重新加载时。
I'll try to explain this a bit better with my code as it sounds complicated. 由于听起来很复杂,我将尝试用我的代码更好地解释这一点。
I have a label on one controller which when pressed, presents a UISearchController
modally. 我在一个控制器上有一个标签,当按下该标签时,
UISearchController
模态形式显示UISearchController
。 when you select a cell, it dismisses the view with an unwind segue and passes the data from the cell back to the previous controller to change the label of the button. 当您选择一个单元格时,它将以顺畅的顺序关闭视图,并将数据从该单元格传递回前一个控制器以更改按钮的标签。
I set the label.text in a variable at the top of the initial controller like so var selectedStation = "Search Stations"
我将label.text设置在初始控制器顶部的变量中,例如
var selectedStation = "Search Stations"
here is my shoddy named function which is used to println
the variable to see if it works which it does: 这是我以次充好名字命名的函数,该函数用于
println
变量以查看其是否起作用:
func updateStuff() {
println("you selected \(selectedStation)")
tableView.reloadData()
}
and i declare the label text in my cellForRowAtIndexPath
like so: 我在
cellForRowAtIndexPath
声明标签文本,如下所示:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("searchFieldCell", forIndexPath: indexPath) as! searchFieldTableViewCell
cell.backgroundView = UIImageView(image: UIImage(named: "red-full"))
cell.destinationLabel.text = selectedStation
}
then in my UISearchController
i have the following to pass that variable back 然后在我的
UISearchController
我将以下内容传递回该变量
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
println(stationArray[indexPath.row])
selectedStation = stationArray[indexPath.row]
self.performSegueWithIdentifier("unwindToSet", sender: self)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.destinationViewController .isKindOfClass(SetAlertController) {
var VC = segue.destinationViewController as! SetAlertController
VC.selectedStation = self.selectedStation
VC.updateStuff()
}
}
essentially my controller retrieves the updated variable but doesn't update it in the tableview, it only updates it in the println. 本质上,我的控制器检索已更新的变量,但不会在表视图中更新它,而只会在println中更新它。
i set up a quick demo project with the following viewcontrollers: 我使用以下视图控制器设置了一个快速演示项目:
class MainViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBAction func unwind(segue: UIStoryboardSegue) {
println("unwinding")
if let sourceViewController = segue.sourceViewController as? ModalViewController {
label.text = sourceViewController.selectedText
}
}
}
tapping on the label results in the modalviewcontroller to show. 点击标签会在modalviewcontroller中显示。 i set this up in storyboard.
我在情节提要中进行设置。
class ModalViewController: UITableViewController {
var selectedText: String?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)!
selectedText = cell.textLabel?.text
performSegueWithIdentifier("unwindToSet", sender: self)
}
}
everything works as expected! 一切都按预期工作! feel free to ask if anything is unclear... you can find the demo project here: https://www.dropbox.com/sh/u2blzmo3ztaaini/AADq8hOMMS71wvBH1eH4Bz_4a?dl=0
随时询问是否不清楚...您可以在此处找到演示项目: https : //www.dropbox.com/sh/u2blzmo3ztaaini/AADq8hOMMS71wvBH1eH4Bz_4a?dl=0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.