textfield in scrollable UITableView losing values

Discussion in 'iOS Programming' started by manman, Aug 31, 2008.

  1. manman macrumors regular

    Aug 18, 2008
    I have a UITableView with some custom UITableViewCell objects. The custom cell just wraps a textField object.

    I have the table getting populated correctly, and all the text fields are showing up. The problem for me is that if I enter text in the text field and scroll it off the screen, when I come back to it the text is gone. Anyone have an idea why the text in the field is missing?

  2. manman thread starter macrumors regular

    Aug 18, 2008
    i figured out my problem, dumb mistake... I didn't realize that cells were aquired again every time they had to be drawn, and I was initializing the text to an empty string at that time.

    That partly lead me to a different problem though. I read in the UITableView class refference that "UITableView caches table-view cells only for visible rows..."

    Is there any way to change that behavior so that a table caches all cells? I have a full table full of text fields that can't all fit on the screen at the same time. When the user finishes entering text in all of these text fields, I want to iterate over all the cells in the table to grab the value in the text field and do some data processing with it.

    With the table only cashing visible cells (and only being able to grab indexPaths to visible cells) i have no idea how I can do this... Again, if anyone has some insight, I would really appreciate it.

  3. supertoto macrumors newbie

    Jul 29, 2008
    Wow~~~, you did a great job!

    Actually, it was written in the guide of table view programming guide, but I still have some other similiar problem to refresh the data in each cell. Fine, I will try to find out why...
  4. manman thread starter macrumors regular

    Aug 18, 2008
    well, I was able to fix my problem but it involved a super ugly hack involving global variables and adding extra info to the custom cell to know what section it's in...

    oh well, it works :p If anyone has a better solution for accessing all cells in a table including the ones that aren't visible, i'd still be very interested. I can't imagine i'm the only one who has ever wanted to iterate over a table that takes up more than the visible screen...

    hope you get your problem worked out too supertoto!
  5. laprej macrumors regular

    Oct 14, 2005
    Troy, NY
    A good thing to do is catch it via the delegate method textFieldShouldReturn. Then you can determine the indexPath of the item modified and then save it to a member variable in your class. Determining the indexPath from inside this method can be a little tricky :eek: - see this post for a good example of how to do it here. Good luck!

Share This Page