Swift NSUserDefaults TableView 多个单元格
Swift NSUserDefaults TableView multiple cells
当我们在 textField 中键入内容并按下按钮时,它会将 textField 的内容保存到 Nsuserdefaults 中。在其他视图控制器中,TableView 读取 NSUserDefaults 并仅显示一个单元格。当我们返回第一个 ViewController 在 textField 中键入其他内容并再次按下按钮时,我们只会在 tableVIew 中再次获得一个单元格。当我们在textField中键入内容时,tableView如何显示多个单元格?
ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func add2(sender: UIButton) {
var connection = self.textField.text
defaults.setObject(connection, forKey: "text")
}
}
ViewController2.swift
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var defaults = NSUserDefaults.standardUserDefaults()
var ourText = String()
var textArray:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.delegate = self
self.tableView.dataSource = self
ourText = defaults.stringForKey("text")!
textArray.append(ourText)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = textArray[indexPath.row]
return cell
}
}
那是因为您每次都存储一个值并覆盖它,所以您应该使用数组并在每次添加时追加该值。
这对你来说应该没问题,在你的 ViewController 中定义数组,然后用它来存储你的数据。
array.append(connection)
NSUserDefaults.standardUserDefaults().setObject(array, forKey: "text")
注意:ViewController
viewWillAppear
中的 等于 NSUserDefaults
的数组,因此当您追加它时,它具有旧值并且然后在保存函数中向其添加新值,例如:
array = NSUserDefaults.standardUserDefaults().objectForKey("text")
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var array:NSMutableArray = NSMutableArray();//create array if you are playing between the two view controllers
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func add2(sender: UIButton) {
var connection = self.textField.text
var dataDic:NSMutableDictionary = NSMutableDictionary();
dataDic.setObject(self.textField.text, forKey: "your key");
// add object in array
array.addObject(dataDic);
//push your array like
var viewController2Obj = ViewController2(nibName: "ViewController2", bundle: nil);
viewController2Obj.arrayText = array;
self.navigationController?.pushViewController(viewController2Obj, animated: true);
}
}
ViewController2
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var arrayText:NSMutableArray!;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayText.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let dataDic = arrayText[indexPath.row] as NSMutableDictionary
cell.textLabel?.text = dataDic.valueforkey("your key") as String;
return cell
}
}
当我们在 textField 中键入内容并按下按钮时,它会将 textField 的内容保存到 Nsuserdefaults 中。在其他视图控制器中,TableView 读取 NSUserDefaults 并仅显示一个单元格。当我们返回第一个 ViewController 在 textField 中键入其他内容并再次按下按钮时,我们只会在 tableVIew 中再次获得一个单元格。当我们在textField中键入内容时,tableView如何显示多个单元格?
ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func add2(sender: UIButton) {
var connection = self.textField.text
defaults.setObject(connection, forKey: "text")
}
}
ViewController2.swift
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var defaults = NSUserDefaults.standardUserDefaults()
var ourText = String()
var textArray:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.delegate = self
self.tableView.dataSource = self
ourText = defaults.stringForKey("text")!
textArray.append(ourText)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = textArray[indexPath.row]
return cell
}
}
那是因为您每次都存储一个值并覆盖它,所以您应该使用数组并在每次添加时追加该值。
这对你来说应该没问题,在你的 ViewController 中定义数组,然后用它来存储你的数据。
array.append(connection)
NSUserDefaults.standardUserDefaults().setObject(array, forKey: "text")
注意:ViewController
viewWillAppear
中的 等于 NSUserDefaults
的数组,因此当您追加它时,它具有旧值并且然后在保存函数中向其添加新值,例如:
array = NSUserDefaults.standardUserDefaults().objectForKey("text")
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var array:NSMutableArray = NSMutableArray();//create array if you are playing between the two view controllers
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func add2(sender: UIButton) {
var connection = self.textField.text
var dataDic:NSMutableDictionary = NSMutableDictionary();
dataDic.setObject(self.textField.text, forKey: "your key");
// add object in array
array.addObject(dataDic);
//push your array like
var viewController2Obj = ViewController2(nibName: "ViewController2", bundle: nil);
viewController2Obj.arrayText = array;
self.navigationController?.pushViewController(viewController2Obj, animated: true);
}
}
ViewController2
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var arrayText:NSMutableArray!;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayText.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let dataDic = arrayText[indexPath.row] as NSMutableDictionary
cell.textLabel?.text = dataDic.valueforkey("your key") as String;
return cell
}
}