Ryohei Arai
Ryohei Arai

Reputation: 121

How do I add texts into each cell using textfield and button?

How can I add texts into tableViewCell? I dragged one textfield and button, and add below them, set tableView and tableviewCell. The identifier for the cell is "Cell". And here is the code I made so far. Ideally, I want to add texts typed inside textfield onto the cell, so that for example, if I add my name, it's gonna be added and if next person adds her name, her name also appears on the cell.

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var textField: UITextField!

var stringArray = [String]()
@IBOutlet weak var AddBUtton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    textField.delegate = self
}

func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldDidEndEditing(textField: UITextField) {
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldClear(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = self.myTableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
    return cell
}

Upvotes: 0

Views: 115

Answers (4)

RJE
RJE

Reputation: 891

Just try to set the textLabel of UITableViewCell

func textFieldShouldReturn(textField: UITextField) -> Bool {
    stringArray.append(textField.text)
    textField.text = nil
    textField.resignFirstResponder()
    table.reloadData() // if possible just reload or insert only the cell
    return true
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = self.myTableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
    cell.textLabel.text = stringArray[indexPath.row]
    return cell
}

Upvotes: 1

Karthik
Karthik

Reputation: 99

Try doing something like this:

@IBOutlet weak var text: UITextField!

@IBOutlet weak var button: UIButton!
@IBOutlet weak var tableView: UITableView!

var name:NSMutableArray = NSMutableArray();

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.tableview.delegate = self;        
    self.tableView.dataSource = self;
}

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

@IBAction func buttonClicked(sender: AnyObject) {
    if(self.text.text == ""){
        let alert = UIAlertController(title: "Alert", message: "Enter a name", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil);
    }
    self.name.addObject(self.text.text!)
    self.text.text = "";
    self.tableView.reloadData();
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell";
    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier);
    if(cell == nil){
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier);
    }

    cell?.textLabel?.text = self.name[indexPath.row] as? String

    return cell!;
}

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

Upvotes: 0

red ranger
red ranger

Reputation: 13

You can create a custom UITableViewCell class which inherits from UITableViewCell. From there you can add a variable to store any text you get from the textfield.

After such, you can use that cell as the new cell inside your cellForRowAtIndexPath method.

Upvotes: 0

NeverHopeless
NeverHopeless

Reputation: 11233

Try this control inside custom UITableViewCell. Every time a name is entered in the text box, add that name to the datasource of this control so it will appear next time in the suggestion list. You have an ease to filter the text as the user types (if needed).

Upvotes: 0

Related Questions