2016-01-28 42 views
0

我正在創建一個應用程序,它在一個文本字段中寫入一個字符串,並將其保存在一個數組中。該數組顯示在嵌入mainViewController中的tableViewController中。情況如下。 http://i.stack.imgur.com/z5uTc.png。 我希望每次點擊綠色的「保存」按鈕時,我在textField中寫入的字符串被保存並顯示在tableView中,並且textField返回空白,因此我可以重新編寫一個字符串,該字符串添加到數組中。在這種情況下,tableView顯示了兩個字符串,我寫在同一個textField中。我試圖編寫兩個viewControllers(mainViewVC和tableVC)的代碼。將textfield的字符串保存到數組中 - swift

MainVC

import UIKit 

class mainVC: UIViewController { 
@IBOutlet var txtField: UITextField! 
var embTableVC: tableVC! 

override func viewDidLoad() { 
super.viewDidLoad() 
// Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
super.didReceiveMemoryWarning() 
// Dispose of any resources that can be recreated. 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if segue.identifier == "embededTableVC" { 
    embTableVC = segue.destinationViewController as! tableVC 
} 
} 

@IBAction func Save() { 
if let Text = txtField.text { 
    if txtField.text == "" { 
     myArray.append(Text) 
     let row = myArray.count-1 
     let indexPath = NSIndexPath(forRow: row, inSection: 0) 
     embTableVC.myTableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } 
} 
txtField.text = "" 
txtField.resignFirstResponder() 
}  
} 

TableVC

import UIKit 

    var myArray = [String]() 

    class tableVC: UITableViewController { 
    @IBOutlet var myTableView: UITableView! { 
didSet { 
    myTableView.dataSource = self 
    myTableView.delegate = self 
} 
} 

override func viewDidLoad() { 
super.viewDidLoad() 
myTableView.dataSource = self 
myTableView.delegate = self 
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "customcell") 
// Do any additional setup after loading the view, typically from a nib. 

} 

override func viewDidAppear(animated: Bool) { 
super.viewDidAppear(animated) 
myTableView.reloadData() 
} 

override func didReceiveMemoryWarning() { 
super.didReceiveMemoryWarning() 
// Dispose of any resources that can be recreated. 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
return 1 
    } 


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
return myArray.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = myTableView.dequeueReusableCellWithIdentifier("customcell", forIndexPath: indexPath) as UITableViewCell 

cell.textLabel?.text = myArray[indexPath.item] 

return cell 
} 

我寫不工作的代碼。我可以編譯和運行我的設備上的應用程序,但是當我點擊綠色的「保存」按鈕時,應用程序崩潰。在「保存」IBAction中,錯誤消息是「致命錯誤:在解包可選值時意外發現零」:embTableVC.myTableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)。 你能幫我嗎? 非常感謝:)

回答

3

您試圖在行甚至未初始化時將行添加到表視圖。你可以做的是,將字符串保存到數組中,並將該數組從MainVC中的prepareForSegue方法傳遞給TableVC。然後使用這個數組來填充TableVC的cellForRowAtIndexPath:方法中的表視圖。

在你的MainVC的Save方法中,你只需要追加數組,那就是你只需要這麼多的代碼。

@IBAction func Save() { 
if let Text = txtField.text { 
    if txtField.text == "" { 
     myArray.append(Text) 
    } 
} 

我假設你的myArray變量是TableVC中的一個屬性。然後在你的prepareForSegue方法中做到這一點。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if segue.identifier == "embededTableVC" { 
    embTableVC = segue.destinationViewController as! tableVC 
    embTableVC.myArray = myArray 
} 
} 

在您的TableVC的cellForRowAtIndexPath方法中,將您設置標籤文本的行修改爲此。

cell.textLabel?.text = myArray[indexPath.row] 
+0

那麼我的代碼有什麼問題?我該怎麼做?我編輯了我的表VC代碼 –

+0

在TableVC中創建一個數組作爲屬性,並在MainVC的prepareForSegue方法中將MainVC中創建的數組傳遞給它。 – Skywalker

+0

使用「保存」功能我沒有問題的代碼。用prepareForSegue函數,我不能寫'embTableVC.myArray = myArray',因爲它說「在embTableVC中沒有成員myArray」。 –

相關問題