How to Retrieve Value from Text Input in Custom TableView Cell?

Discussion in 'iOS Programming' started by Inkreaser, Jan 24, 2010.

  1. Inkreaser macrumors newbie

    Joined:
    Nov 25, 2009
    #1
    Hi,

    I have followed a tutorial and successfully created a custom tableview cell in Interface Builder. I have put a text field into my cell and am able to enter text into this when I run the app on the simulator.

    My first question is, is this the best way to put a text field into a tableview cell? I want people to be able to enter text in the way that they do when entering a new contact's name in Address Book.

    My second questions is, how do you retrieve the value of what is entered into the text field? Even if only at first to write it to NSLog. My custom cell is defined in a class called myCustomCell, and the table view is implemented in myTableViewControlller.

    Any help would be appreciated. Thank you.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Sounds fine to me.

    I'd suggest defining an IBOutlet property for your UITextField in your myCustomCell class (don't forget to connect it) and then, as long as you have access to your cell, you should have access to your textField.
     
  3. Inkreaser thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #3
    Thanks Dejo! I've continued and managed to communicate with the text field of my custom cell, by using this line in the cellForRowAtIndexPath method:

    cell.myTextField.text = @"Test";

    This sets the text of the text field in each row, which is a good start. However, I'm unsure how to do the reverse of this, retrieving the value of the text field in the didSelectRowAtIndexPath method. Do you know how to do this? Sorry if this is a simple thing to do, I'm just not sure of the syntax. Thanks again for your help so far.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Actually, it is pretty simple. If you're not sure how this is done, perhaps it is time to step away from the real coding and go (re)learn the basics of Objective-C.
     
  5. Inkreaser thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #5
    Ok, I'm taking a hands-on approach and learning as I go (by 'go', I mean by building an app). I'd be really grateful if someone could help with the solution to this if they know it.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Another skill you'll want to learn is how to find and read through pre-existing information. You'll find that if you can master this skill, you'll get further much faster than asking for solutions and waiting for answers.
     
  7. Inkreaser thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #7
    I can see that you've mastered the art or patronisation.

    The fact that I'm resorting to posting my question here is because I've not found the answer in the learning material I've been reading, or from scouring the internet. I'm not expecting anyone to write my app for me, but It's my understanding that these forums are here for people to ask for help.

    Just because I'm asking what you consider to be a simple question, doesn't mean I shouldn't get a decent answer. If you're not going to help, please refrain from responding, otherwise this thread will just turn into another dead end for people carrying out future searches for the answer to this question.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    I apologize if I sounded patronizing. I was only trying to help, without just giving you code to copy-and-paste. I'll refrain from any further posts in this thread.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    The problem of retrieving text from textfields in a table is a bit complicated. The problem is that the table recycles your cells and also can release them, all without any notification to your code that this is happening.

    I haven't written code to implement this particular task but I've written a lot of table code. I'll think out loud here how I would do it.

    With other, simpler, controls, like say a UISwitch, you listen to its action method and then update your data model. We can do something similar. Either update the data model on every key press or maybe when the text field gives up first responder status. There is a delegate callback for each key press so that's probably how I'd do it. Not sure about how to catch the text field giving up first responder status.

    One tricky bit is telling which text field is getting the key press. You can use the tag in the text field and set it to the row. That's the simplest. Otherwise you can use a cell subclass that keeps track of its indexPath.

    All right that's most of it.
     

Share This Page