Problems adding a UITableView inside a UIViewController in Swift

So, I'm trying to create a single on-screen application, where I start with the UIViewController. In addition, there is a UITextfield, button, and UITableView.

Essentially, the intended functionality of the application is that the user enters a word into a UITextField, clicks a button, and displays it on a UITableView.

I never had a problem with this when the button adds UITextField entries to the UITableViewController on another screen, but for some reason I am having problems with the UITableView built into the UIViewController ... On my storyboard, I made a UITableView link as a delegate and UIViewController data source, so this should not be a problem.

Any ideas? My code is published below.

import UIKit var groupList:[String] = [] class ExistingGroups: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var addGroup: UITextField! @IBAction func addGroupButton(sender: AnyObject) { self.view.endEditing(true) var error = "" if addGroup.text == "" { error = "Please enter a Group!" } else { groupList.append(addGroup.text) } if error != "" { var alert = UIAlertController(title:"Error In Form", message: error, preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title:"OK", style: .Default, handler: { action in self.dismissViewControllerAnimated(true, completion:nil) })) self.presentViewController(alert, animated:true, completion:nil) } addGroup.text = "" } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. addGroup.placeholder = "Enter Group Name" } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func touchesBegan(touches:NSSet, withEvent event:UIEvent) { super.touchesBegan(touches, withEvent: event) self.view.endEditing(true) } func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore. { textField.resignFirstResponder() return true; } func tableView(tableView:UITableView, numberOfRowsInSection section: Int) -> Int { return groupList.count } func numberOfSectionsInTableView(tableView:UITableView) -> Int { return 1 } func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell { var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("groupcell") as UITableViewCell cell.textLabel?.text = groupList[indexPath.row] return cell } } 
+7
ios uitableview uiviewcontroller swift
source share
2 answers

First of all, create an IBOutlet for the table view as follows:

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { var groupList = [String]() @IBOutlet weak var addGroup: UITextField! @IBOutlet weak var tableView: UITableView! //<<-- TableView Outlet // Rest of your code.. } 

After that, do this in your viewDidLoad method:

 override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "groupcell") self.addGroup.delegate = self tableView.delegate = self tableView.dataSource = self } 

After that, you can reload your tableView after adding the item to groupList . Here you can do this:

 @IBAction func addGroupButton(sender: AnyObject) { // Your Code } else { groupList.append(addGroup.text) self.tableView.reloadData() } //Your Code } 

And update this function:

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

And you can check the final code that I created for you, HERE .

+21
source share
 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. addGroup.delegate = self // You can do this in storyboard also addGroup.placeholder = "Enter Group Name" } @IBAction func addGroupButton(sender: AnyObject) { self.view.endEditing(true) var error = "" if addGroup.text == "" { error = "Please enter a Group!" } else { groupList.append(addGroup.text) //reload the table after adding text to array self.tableView.reloadData(); } if error != "" { var alert = UIAlertController(title:"Error In Form", message: error, preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title:"OK", style: .Default, handler: { action in self.dismissViewControllerAnimated(true, completion:nil) })) self.presentViewController(alert, animated:true, completion:nil) } addGroup.text = "" } 
0
source share

All Articles