0

I have some text fields and labels in cells in a tableView that I need to reset to empty on a button press. I cannot do this with a reloadData call and setting everything in cellForRowAtIndexPath to @"" because there are situations where the table will be reloaded and i need to retain values.

So the only option is for me to somehow loop through every textfield or label and individually set their texts to @"". But how do I access a cell, then pull out the textfield or label? keep in mind my Table also consists of different types of custom cells.

I figured it would be best to do it with tags maybe but I don't know how to access a cell's 'viewWithTag' call. If there even is such a thing.

edit added block of code where textfields are created

    BaseTableViewCell *baseCell = (BaseTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (baseCell == nil){
        baseCell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];

        BaseTextField *rightText = [[BaseTextField alloc] initWithFrame:CGRectMake(150, 7, 150, 31)];
        rightText.placeholder = tableValues.key;
        rightText.font = [UIFont systemFontOfSize:14];
        rightText.tag = (indexPath.section + 1) + (indexPath.row + 1);
        rightText.delegate = self;
        [baseCell addSubview:rightText];
    }
    NSLog(@"resetting text");
    baseCell.selectionStyle = UITableViewCellSelectionStyleNone;
    baseCell.backgroundColor = [UIColor clearColor];
    baseCell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    baseCell.textLabel.text = tableValues.value;

    return baseCell;
}
JMD
  • 1,540
  • 2
  • 24
  • 56
  • Do you want to do this for text field and labels in all the rows of the table? (not necessarily every text field and label in those cells, but for certain ones in every row)? – rdelmar Feb 08 '13 at 18:53
  • yes. every row, but not necessarily every label or textfield – JMD Feb 08 '13 at 18:55
  • The calculation for tag is wrong; will only work if you only have 1 section. e.g. section 0; row 3 equals section 2 row 2: (0+1) + (2+1) = 4; (1+1)+(1+1)=4 – Dave Feb 08 '13 at 19:27
  • Also, "rightText" is only populated for newly created cells. If you re-use a cell; the 'rightText' part of your cell is not populated. Is that intentional? – Dave Feb 08 '13 at 19:32
  • @Dave the cells are set to be re-used, so when it needs a new cell, the text field will either already be there, or created. – JMD Feb 08 '13 at 21:38
  • @Jesse, I was just pointing out that you only populate it with data when it's created. If it's always static content, why not create it in your BaseTableViewCell subclass. – Dave Feb 08 '13 at 21:48
  • the labels at the start are always empty as well as the textfields. a user can put data in a text field via keyboard and can put values in the labels by selecting the row, going to another screen, making a choice, and then the label is populated with that choice. i just need a way to wipe all that clean. nothing i want to clear is really being populated by a data model. its all user input. – JMD Feb 08 '13 at 22:05

5 Answers5

2

Issues like this tend to resolve themselves if you have a "data model" and treat it as "the truth". In other words, always populate every cell of your tableview from your data. You can have a simple array or two, or something more complex. If you want to zap anything, zap the values in the model and reload the table.

This SO answer will help you loop through your tableview if you want to do things that way; however, I recommend you update your data and call reload tableview.

Community
  • 1
  • 1
Dave
  • 7,552
  • 4
  • 22
  • 26
  • this is actually how I have things set up. I have an array of data that populates everything. However my textfields start empty simply because i don't assign any text to it. But it has data values associated with it that i cannot erase. – JMD Feb 08 '13 at 19:01
  • How do they go from empty to populated? You update your model and reload the table? Could you simply introduce a flag like "hideLabels"; and in your cellForRowAtIndexPath check the flag to determine if you use data from your model or blank/hide the label? The rule remains - always ask your model for the data. This is what folks will expect when maintaining your app. – Dave Feb 08 '13 at 19:07
0

use UITableViews

- (NSArray *)visibleCells

which gives you all the cells then loop through the cells subviews and reset the labels.

mgr
  • 342
  • 2
  • 7
  • how do you loop through the cell's subviews? – JMD Feb 08 '13 at 18:57
  • have a look here http://stackoverflow.com/questions/2746478/how-can-i-loop-through-all-subviews-of-a-uiview-and-their-subviews-and-their-su – mgr Feb 08 '13 at 19:01
0

There is a UITableView property called visibleCells, which is an array of all of the cells you can see. Is that all that you need to change? Otherwise you would need loop through all of the cells and set the text fields to @"" like you said, using -numberOfSections, -numberOfRowsInSection, and -cellForRowAtIndexPath:

Chris C
  • 3,221
  • 1
  • 27
  • 31
  • No, i need to loop through all the cells using numberofsections, numberofrowsinsect etc. I just didnt know how to get access to the cells. tableView.visibleCells seems like it would work but im still confused on how i would loop through. i wont have access to # of sections and rows etc with just an array of visible cells – JMD Feb 08 '13 at 18:56
  • Visible cells is just if you wanted to use the cells on screen. It's an array so you can iterate through it like any array. If you need to do all of them, you can find out the total number of cells by using those numberOf.. methods, and then call cellForRowAtIndexPath: on each cell (row) number. – Chris C Feb 08 '13 at 19:10
0

UIView has a tag property you can set.

UITextField *textField = [UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 28)];
textField.tag = 20;

Generally, UITableView will reuse cells so you may have an issue when the cell reload different content.

You should look at managing your data separately from the table view.

bbarnhart
  • 6,620
  • 1
  • 40
  • 60
0

If I understand you're question correctly, you don't do this by accessing the cells, but by changing the array that you use to populate the cells. Set whatever field in your custom objects, or a value in a dictionary (if you're using an array of dictionaries) to @"".

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • for the labels, this is possible. But its not for my textfields. I dont initialize any text inside the fields from the start (even though there is hidden data for them) – JMD Feb 08 '13 at 19:00
  • @JesseDurham, how do you get the value into the text field? By user action? If so, then that value should be added to your array anyway, so that when you scroll away from them and come back, the value will still be there. – rdelmar Feb 08 '13 at 19:02
  • the text fields currently seem to be doing that already on their own. i can even change views on the nav controller and go back, reload data, and the text fields hold the user typed data... I dont add the values typed in until another condition is met. (a different button) – JMD Feb 08 '13 at 19:04
  • @JesseDurham, I don't know how that's possible without grabbing that value from somewhere. Where does that value come from when the table reuses a cell? Are these dynamic table view cells? – rdelmar Feb 08 '13 at 19:06
  • No, they are re-usable cells. TBH I dont know how its holding the values either. But I noticed if i try the method of reloading data and setting textfield text to @"" it doesn't change. it still holds user inputted text. I'll update the original question with my cell creation – JMD Feb 08 '13 at 19:09
  • @JesseDurham, how many rows do you have in your table now? Is it enough that the table needs to reuse cells. – rdelmar Feb 08 '13 at 19:15
  • currently there are about 12 with hardcoded data. However this is dynamic and eventually there could be a situation where they need to be reused. – JMD Feb 08 '13 at 19:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24207/discussion-between-rdelmar-and-jesse-durham) – rdelmar Feb 08 '13 at 19:21