当我按下 swift 中的按钮并使用文本字段中的一些文本时,我可以将新单元格添加到表格视图吗?

Can I add a new cell to a tableview, when I press a button in swift, with some text from a textfield?

所以刚开始学习swift,有点卡在这上面。

我有这个 table 视图的示例。代码中的数组中插入了 3 个文本...但我想用我放入文本字​​段中的一些文本来完成该数组... -> @IBOutlet weak var inputMessage: UITextField! ,我想在之后添加文本我按下一个按钮:@IBAction func sendMsg(sender: AnyObject) ...我不知道如何在 table 中为每个要插入的文本创建一个单元格 ...

可以这样做吗...?如果是的话,你能给一些提示吗……?

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    //table view
    @IBOutlet weak var tableView: UITableView!
    //input field
    @IBOutlet weak var inputMessage: UITextField!
    //text arrays

    var textArray: NSMutableArray! = NSMutableArray()
    //var input = inputMessage.text


    //push the button
    ///when you press the button create a label and put in a cell view
    @IBAction func sendMsg(sender: AnyObject) {

        var input = inputMessage.text
        self.textArray.addObject(input)

        //make rows change their dimensions
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 44.0

        self.textArray.addObject(input)


         func viewDidLoad() {
            super.viewDidLoad()
        }

        }

    // the view did load function
    override func viewDidLoad() {
        super.viewDidLoad()

        self.textArray.addObject("Before You Say I Can'T, Make Sure You'Ve Tried.")

        self.textArray.addObject("-I'm a mirror. If you're cool with me, I'm cool with you, and the exchange starts. What you see is what you reflect. If you don't like what you see, then you've done something. If I'm standoffish, that's because you are.")

        self.textArray.addObject("It seems like once people grow up, they have no idea what's cool.")

        var input = inputMessage.text
        //make rows change their dimensions
        self.tableView.rowHeight = UITableViewAutomaticDimension

        self.tableView.estimatedRowHeight = 44.0

    }


    // the did received memory warning
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: Table View Delegate

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        cell.textLabel?.text = self.textArray.objectAtIndex(indexPath.row) as? String

        return cell

    }
}

谢谢!

将新元素添加到 textArray 后重新加载您的 tableView 就像这样:

self.textArray.addObject(input)
self.tableView.reloadData()

并且您将对象添加到 textArray 两次。

所以删除self.textArray.addObject(input)

你的操作方法是:

@IBAction func sendMsg(sender: AnyObject) {

    var input = inputMessage.text

    //make rows change their dimensions
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 44.0

    self.textArray.addObject(input)
    tableView.reloadData()

}