简体   繁体   中英

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
}

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
}

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.

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).

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;
}

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