2016-08-23 44 views
-1

這是代碼:如何將字符串保存到表格視圖?

import UIKit 

var reminder = [String]() 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var textField: UITextField! 

    @IBAction func checkMarkTapped(sender: AnyObject) { 
     reminder.append(textField.text!) 
     textField.text = "" 
    } 

    @IBAction func addReminder(sender: AnyObject) { 
    } 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell:CoustemCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CoustemCell 
     cell.tableViewLabel.text = reminder[indexPath.row] 
     return cell 
    } 
} 

所以我當過你按checkMarkTapped它將添加任何被放置在文本框的表視圖。但是我放入checkMarkTapped的代碼假設可以正常工作,但會一直崩潰。

那麼我做錯了什麼?

+1

它在崩潰時說什麼? – EmilioPelaez

+1

您是否已將文本字段分配給文本字段插座? – Paulw11

回答

0
import UIKit 

class ViewController: UIViewController 
{ 
    var reminder = [String]() 
    @IBOutlet weak var tbl_Update: UITableView! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() 
    { 
     super.didReceiveMemoryWarning() 
    } 

    @IBOutlet weak var textField: UITextField! 

    @IBAction func checkMarkTapped(sender: AnyObject) 
    { 
     reminder.append(textField.text!) 
     textField.text = "" 
     tbl_Update.reloadData() 
    } 

    @IBAction func addReminder(sender: AnyObject) 
    { 

    } 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell:CoustemCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CoustemCell 
     cell.tableViewLabel.text = reminder[indexPath.row] 
     return cell 
    } 

} 
+0

如果你仍然有問題,請參閱鏈接代碼https://github.com/shemona-ios/TableViewUpdateValue.git – Shemona

相關問題