为什么我的变量在 printLn 中发生变化但在 tableview 中没有变化 Swift
Why is my variable is changing in printLn but not in the tableview Swift
我花了很长时间试图解决这个问题,但没有下定决心。
我终于到了一个地步,当我展开 segue 时,我从一个控制器中提取数据并将其移动到目标控制器,但是,当变量仅在 println
中重新加载时但不在表格视图中。
我会尝试用我的代码更好地解释这一点,因为它听起来很复杂。
我在一个控制器上有一个标签,按下时会模态显示 UISearchController
。当您 select 一个单元格时,它会通过展开 segue 关闭视图,并将单元格中的数据传递回前一个控制器以更改按钮的标签。
我在初始控制器顶部的变量中设置了 label.text 像这样
var selectedStation = "Search Stations"
这是我的劣质命名函数,它用于 println
变量以查看它是否有效:
func updateStuff() {
println("you selected \(selectedStation)")
tableView.reloadData()
}
我在 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
}
然后在我的 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()
}
}
基本上我的控制器检索更新的变量但不在表视图中更新它,它只在 println 中更新它。
我使用以下视图控制器设置了一个快速演示项目:
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
}
}
}
点击标签会显示 modalviewcontroller。我在情节提要中设置了它。
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)
}
}
一切正常!随时询问是否有任何不清楚...
你可以在这里找到演示项目:https://www.dropbox.com/sh/u2blzmo3ztaaini/AADq8hOMMS71wvBH1eH4Bz_4a?dl=0
我花了很长时间试图解决这个问题,但没有下定决心。
我终于到了一个地步,当我展开 segue 时,我从一个控制器中提取数据并将其移动到目标控制器,但是,当变量仅在 println
中重新加载时但不在表格视图中。
我会尝试用我的代码更好地解释这一点,因为它听起来很复杂。
我在一个控制器上有一个标签,按下时会模态显示 UISearchController
。当您 select 一个单元格时,它会通过展开 segue 关闭视图,并将单元格中的数据传递回前一个控制器以更改按钮的标签。
我在初始控制器顶部的变量中设置了 label.text 像这样
var selectedStation = "Search Stations"
这是我的劣质命名函数,它用于 println
变量以查看它是否有效:
func updateStuff() {
println("you selected \(selectedStation)")
tableView.reloadData()
}
我在 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
}
然后在我的 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()
}
}
基本上我的控制器检索更新的变量但不在表视图中更新它,它只在 println 中更新它。
我使用以下视图控制器设置了一个快速演示项目:
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
}
}
}
点击标签会显示 modalviewcontroller。我在情节提要中设置了它。
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)
}
}
一切正常!随时询问是否有任何不清楚... 你可以在这里找到演示项目:https://www.dropbox.com/sh/u2blzmo3ztaaini/AADq8hOMMS71wvBH1eH4Bz_4a?dl=0