简体   繁体   中英

UITextField and UITableView on a single view controller

I'm trying to make a view controller that has one text field that populates the tableview below, ideally the user will be able to continue to add to the tableview without jumping between two views.

I previously had it working with the text field on one view that populates a UITableView and used prepareForSegue to push the data to the table, but I haven't been able to get it to work with just one view.

Can anyone please point out where I'm going wrong or push me to a tutorial / documentation to help?

Edit: Clarity

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

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

var items: [String] = ["Pls", "work", "pls", "work", "pls"]
var foodGroup: FoodGroup = FoodGroup(itemName:"")

//var foodGroup: [FoodGroup] = []

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

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

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

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

    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.textLabel.text = self.items[indexPath.row]
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("Selected cell #\(indexPath)")
}

func addFood(sender: AnyObject!) {
    if (countElements(self.textField.text) > 0) {
        self.foodGroup = FoodGroup(itemName: self.textField.text)
    }
}

@IBAction func addFoodToList() {
    let source = FoodGroup
    let foodGroup:FoodGroup = source.foodGroup

    if foodGroup.itemName != "" {
        self.foodGroup.append(foodGroup)
        self.tableView.reloadData()
    }
}

}

It seems like your intention here is to have your dataSource be an array of FoodGroup objects. If this is indeed the case you can get rid of your foodGroup instance variable and update your items definition to be like so:

var items = [FoodGroup]()

then in addFoodToList:

if self.textField.text != "" {
    let foodGroup = FoodGroup(itemName: self.textField.text)
    self.items.append(foodGroup)
    self.tableView.reloadData()
}

and finally in cellForRowAtIndexPath:

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

let foodGroup = self.items[indexPath.row] as FoodGroup
cell.textLabel.text = foodGroup.itemName

return cell

Also I don't quite see the intention of your the addFood(sender: AnyObject!) function. Looks like cruft. I would get rid of it. Good luck!

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