2015-09-01 92 views
0

當我們在textField中鍵入內容並按下按鈕時,它將textField的內容保存到Nsuserdefaults中。在其他View Controller中,TableView讀取NSUserDefaults並僅顯示一個單元格。當我們回到第一個ViewController來在textField中輸入其他內容時,我們再次按下按鈕,我們只能再次得到tableVIew中的一個單元格。當我們在textField中輸入內容時,tableView如何顯示多個單元格?Swift NSUserDefaults 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

那是因爲你是存儲一個值,並且每次重寫它,你應該使用陣列和追加每次添加時都會顯示值。

這應該適合你,在你的ViewController定義數組中,然後用它來存儲你的數據。

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

注:ViewControllerviewWillAppear陣列等於NSUserDefaults所以當你添加它,它具有原有值,然後添加新的價值,它的保存之類的函數:

array = NSUserDefaults.standardUserDefaults().objectForKey("text") 
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 

} 


} 
相關問題