简体   繁体   English

快速按下带有文本字段中某些文本的按钮时,是否可以向表格视图添加新单元格?

[英]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! 从代码中的数组中插入了3个文本...但是我想用我在文本字段中放置的一些文本来完成该数组...-> @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 ... ,并且我想在按下按钮后添加文本: @IBAction func sendMsg(sender: AnyObject) ...我不知道如何为要插入的每个文本在表格中创建一个单元格...

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: 在向textArray添加新元素后,只需重新加载tableView即可, textArray所示:

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

And you are adding your object into textArray two times. 然后将对象两次添加到textArray

So remove self.textArray.addObject(input) 因此删除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()

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表格视图单元格中收集文本字段文本(快速) - Gather textfield text from a tableview cell (Swift) 从TableView单元格Swift将文本传递到TextField - Passing text to TextField from tableview cell swift 从文本字段在 tableView 中添加新单元格 - Add new cell in tableView from textfield 迅速解释为什么从解析中检索数据时我必须按两次按钮才能在Tableview中加载数据 - Swift why when Retrieving data from Parse I have to press button twice to load data in tableview 如何通过单击添加来添加单元格,该单元格将采用 TextField 值并在该表格视图中创建一个带有该文本的单元格,如下图所示在 Swift/Obj-C 中? - How do I add a cell by click on add that will take TextField value and create a cell with that text in that tableview like below image in Swift/Obj-C? 当我按下 tableview 中的活动按钮时 - when I press the button active in tableview 当我从TableView上的按钮操作中删除单元格时,应用程序崩溃了-iOS Swift - When i remove cell from TableView on button action the app is crashed - iOS Swift 当我从 tableview 单元格中删除单元格时,未删除的单元格会重复 SWIFT - When I delete cell from tableview cells that are not deleted are duplicated SWIFT 每当我在textField中输入新值时,如何更新tableView单元格的标签? - How to update tableView cell's labels when every time i enter new value in textField? 如何通过按“添加”按钮添加新单元格并将文本保存到Coredata中 - How to add a new cell by press a Add button and save the text into Coredata
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM