JMD
JMD

Reputation: 1590

clear text in a textfield or label in a cell by button action

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;
}

Upvotes: 0

Views: 1112

Answers (5)

rdelmar
rdelmar

Reputation: 104082

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 @"".

Upvotes: 0

Dave
Dave

Reputation: 7717

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.

Upvotes: 2

bbarnhart
bbarnhart

Reputation: 6710

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.

Upvotes: 0

Chris C
Chris C

Reputation: 3251

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:

Upvotes: 0

mgr
mgr

Reputation: 342

use UITableViews

- (NSArray *)visibleCells

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

Upvotes: 0

Related Questions