Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I add the UIAlertView text input to table view

I want to add the UIAlertView text input to the table view so how can I do that? thanks!

Chris Shaw
Chris Shaw
26,676 Points

Hi Matthew,

Could you please elaborate on how the UIAlertView will be triggered? Do you want a button to create the alert view or pressing on the table cell itself?

Basically you press a button which opens up the UIAlertView which you then enter text in the text field and then press the button 'continue'

So what im trying to do here is add the user input to the table view.

prntscr.com/5un7xw

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Matthew,

See the below code.

I created this quickly within a test app, what's going on is I've declared an Array that contains values of the type String, next I've setup the table view as we normally would by referencing the count of the users array within numberOfRowsInSection which by default is zero.

Next in the cellForRowAtIndexPath method I simply request the value for each index item and store it in the text property of textLabel, finally I've hooked up an @IBAction to an UIBarButtonItem which I've added to the navigation bar by embedding the UITableViewController within a UINavigationController as seen in the below picture.

IBAction Connection

Inside the @IBAction I've created a new UIAlertView with the style PlainTextInput, finally I've got an alertView method which gets called when either the Cancel or Done buttons are pressed, if the Done button is pressed the buttonIndex parameter is set to 1 which we use to append the new name to the users array and reload the tableView data for the controller.

Hopefully that wasn't too much of a mouthful to take in but that should help.

class TableViewController: UITableViewController, UIAlertViewDelegate {

    var users: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

        // Set the label text as the users name
        cell.textLabel!.text = users[indexPath.row]

        return cell
    }

    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 1 {
            users.append(alertView.textFieldAtIndex(0)!.text)
            tableView.reloadData()
        }
    }

    @IBAction func addUser(sender: AnyObject) {
        var alert = UIAlertView(title: "Enter users name", message: nil, delegate: self, cancelButtonTitle: "Cancel")

        alert.addButtonWithTitle("Done")
        alert.alertViewStyle = .PlainTextInput
        alert.show()
    }

}

Hey how can I private message you?

Chris Shaw
Chris Shaw
26,676 Points

Sadly that feature doesn't exist on Treehouse, is there something in particular that you need help with?

Hey so i added your code, but it's not adding the text to the table.. nothing happens. http://pastebin.com/L8gMj9sk

That's the code I have

Also, for the : class SecondViewController: UIViewController, UIAlertViewDelegate{

part, whenever i include the UITableViewController part it gives me an error.. please help

Chris Shaw
Chris Shaw
26,676 Points

You're class should be bound to a UITableViewController whether it be a parent view or a child view, for clarity I've uploaded my entire test project which you can download at the below URL which should help you out.

https://www.dropbox.com/s/9wzov0i3rmu2jw7/AddUserToTable.zip?dl=0

Hmm yes, your's works. Can I have your email just so I can send you the link to my project, I dont really want it being exposed publicly. Thanks

Chris Shaw
Chris Shaw
26,676 Points

Sure, send it to removed

I sent it 6 hours ago, did you get it?

Chris Shaw
Chris Shaw
26,676 Points

Hi Matt,

Just about to look at it now as I was at work earlier and only got home a couple of hours ago, I'll keep you posted.

Oh ok, sorry to bother you :)

Also, I see how yours worked, but I don't want to use a tableviewcontroller, as i can't add a toolbar to it and it needs to be neat and simple with a flat design. How can i do what u did but with a toolbar on a single view..?