重用表视图变量 Swift [英] Reuse Table View variables Swift
问题描述
这是表格视图:
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
let too: AnyObject = rowData ["time"] as NSString
var name: String = rowData["time"] as String
var formattedPrice: String = rowData["date"] as String
var alert: UIAlertView = UIAlertView()
alert.title = name
alert.message = formattedPrice
alert.addButtonWithTitle("Ok")
alert.show()
println ("hi")
println (too)
}
我需要在另一个视图控制器中引用这些变量.我无法将上面的声明包含在此内容中:
I need to reference these variables in another view controller. I have not been able to fit that statement above in this:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
var svc = segue!.destinationViewController as secondViewController;
svc.toPass = textField.text
}
}
我正在尝试关闭单元格点击.来自 http://jamesleist.com/ios-swift-passing-data-between-视图控制器/
I am trying to segue off cell click. from http://jamesleist.com/ios-swift-passing-data-between-viewcontrollers/
推荐答案
如果不是很多数据,我在视图控制器之间传递数据的策略是将值存储在 NSUserDefaults
.
If it isn't a lot of data, the strategy I use to pass data between view controllers is to store the value in NSUserDefaults
.
设置值:当您第一次获取数据时,将其存储在 NSUserDefaults
中.
Setting A Value: When you first get the data, store it in NSUserDefaults
.
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() //This class variable needs to be defined every class where you set or fetch values from NSUserDefaults
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
let too: AnyObject = rowData ["time"] as NSString
var name: String = rowData["time"] as String
var formattedPrice: String = rowData["date"] as String
defaults.setObject(rowData, forKey: "rowData")
defaults.setObject(too, forKey: "too")
defaults.setObject(name, forKey: "name")
defaults.setObject(formattedPrice, forKey: "formattedPrice")
defaults.synchronize() //Call this after you're done editing defaults, it saves your change to the disk
var alert: UIAlertView = UIAlertView()
alert.title = name
alert.message = formattedPrice
alert.addButtonWithTitle("Ok")
alert.show()
println ("hi")
println (too)
}
获取值:当您需要获取值时,只需从 NSUserDefaults
中获取即可.
Fetching A Value: When you need to get the values, just grab it from NSUserDefaults
.
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.dictionaryForKey("rowData") as? NSDictionary
defaults.objectForKey("too") as? String
defaults.objectForKey("name") as? String
defaults.objectForKey("formattedPrice") as? String
这样做可以让您访问任何类中存储的值,并允许数据在应用程序关闭和重新打开后保留.如果要在应用关闭后清除数据,在 AppDelegate applicationWillTerminate(application: UIApplication)
函数中,调用 removeObjectForKey 函数为之前设置的每个键.
Doing that gives you access to the stored values in any class and allows the data to persist after the app is closed and reopened. If you want to clear the data after the app closes, in AppDelegate applicationWillTerminate(application: UIApplication)
function, call the removeObjectForKey function for each key previously set.
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("rowData")
defaults.removeObjectForKey("too")
defaults.removeObjectForKey("name")
defaults.removeObjectForKey("formattedPrice")
defaults.synchronize()
NSUserDefaults 的有用来源:
NSUserDefulats
类参考:链接 这里.
这篇关于重用表视图变量 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!