Swift NSUserDefaults TableView 多个单元格

Posted

技术标签:

【中文标题】Swift NSUserDefaults TableView 多个单元格【英文标题】:Swift NSUserDefaults TableView multiple cells 【发布时间】:2015-09-01 13:24:48 【问题描述】:

当我们在 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    
     
 

【问题讨论】:

【参考方案1】:
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





【讨论】:

【参考方案2】:

那是因为您每次都存储一个值并覆盖它,所以您应该使用数组并在每次添加时附加该值。

这对你来说应该没问题,在你的 ViewController 中定义数组,然后用它来存储你的数据。

 array.append(connection)
 NSUserDefaults.standardUserDefaults().setObject(array, forKey: "text")

注意: ViewController viewWillAppear 中的数组等于 NSUserDefaults 所以当你追加它时,它具有旧值,然后你在保存函数中将新值添加到它喜欢:

array = NSUserDefaults.standardUserDefaults().objectForKey("text")

【讨论】:

以上是关于Swift NSUserDefaults TableView 多个单元格的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 将字典数组保存到 NSUserDefaults

Swift:将 NSDictionary 保存到文件中,因为 NSUserDefaults 会变慢

Swift:NSUserDefaults 没有立即设置

无法从 NSUserDefaults 检索数据到今天的扩展(Swift)

使用 Swift 将类中的结构保存到 NSUserDefaults

Swift 将图像(截图)保存到 nsuserdefaults