Swift NSUserDefaults TableView多个单元格

时间:2015-09-01 13:24:49

标签: ios swift tableview

当我们在textField中输入内容时,当我们按下按钮时,它会将textField的内容保存到Nsuserdefaults中。在其他View Controller 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    
     }
 }

2 个答案:

答案 0 :(得分:2)

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

}


}

答案 1 :(得分:1)

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

这应该适合你,在你的ViewController定义数组中,然后用它来存储你的数据。

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

注意:<{strong>} ViewController viewWillAppear将数组等于NSUserDefaults,因此当您追加它时,它具有旧值,然后您添加新值保存函数中的值,如:

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