简体   繁体   中英

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

so I have just started to learn swift, and I am a bit stuck at this.

I have this example with this table view . There are 3 texts inserted in an array from the code ... but I want to complete that array with some text that I put in a text field... -> @IBOutlet weak var inputMessage: UITextField! , and I want to add the text after I press a button : @IBAction func sendMsg(sender: AnyObject) ... I don't know how to create a cell in the table for each text I want to insert ...

It is possible to do that ... ? If yes, cand you give some tips ... ?

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

    }
}

Thank you!

Just reload your tableView after adding new element to textArray Like this:

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

And you are adding your object into textArray two times.

So remove self.textArray.addObject(input)

And your action method will be:

@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()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM