NSCool
NSCool

Reputation: 229

How to Clear Text in UITextField of CustomCell of UITableview?

I have a UITextField in My CustomCell of UITableview and i am using this UITableview as Subview of Mainview.There is another UITableview Which is also on my MainView but i am using this UITableview as Subclass of my MainView.Below image will also be hlepfull to Understand my problem.

enter image description here

As image show that above UITableview is my SubClass UITableviw and Below one Which is single row UITableview with CustomCell having UITextField. Now i want that when i Click on any Cell of SubClass UITableview my UITextField Text is Clear which i have in CustomCell of UITableview.

Note:- To Make my question more easir i have some code which works for me when i have UITextfield Simply on my MainView (not in CustomCell of UITableview).here is my Code. In Mainview.h file

#import <UIKit/UIKit.h>
#import "Inputtableview.h"
@interface testingViewController : UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITextField *txtfinput;
Inputtableview *inputview;
IBOutlet UITableView *inputtbl; 
}
@property (nonatomic, retain) UITextField *txtfinput;
@end

And then in ViewDidload of Mainview.m file

- (void)viewDidLoad {
if (inputview == nil) {
    inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
    inputview.view = inputview.tableView;
    inputview.myAccessToMaintxtf = txtfinput;
[super viewDidLoad];
}

Now in my Subclass of UITableview.h file

#import <UIKit/UIKit.h>
@interface Inputtableview : UITableViewController<UITableViewDelegate,UITableViewDataSource> {
}
@property (nonatomic, assign) UITextField *myAccessToMaintxtf;
@end

And Finally in My SubClass UITableview.m file

@implementation Inputtableview
@synthesize myAccessToMaintxtf; 


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
 myAccessToMaintxtf.text=@"";
 }

As i mention Earliar the Above Code works Fine when I have TextField in my MainView,But Did't know How to fix it When i Want to Clear the text of UITextField in CustomCell of UITableview.Any help Will be Appriated.Thanx in Advance.

Upvotes: 1

Views: 771

Answers (2)

LuisEspinoza
LuisEspinoza

Reputation: 8538

Ok, I can't see how you are connecting "myAccessToMaintxtf"to the textfield in your CustomCell. What I use for getting subviews from my custom cells are "tags". A tag can be used like an identifier for your subviews. So, if you are using a xib you can set the tag in the settings tab of your textfield, and if you are creating the textfield by code you can set the tag like:

 myAccessToMaintxtf.tag = 11; //11 is and example number, use the number that you want

Then when you want to access to your textfield use:

theTextFieldToClear = (UITextField*)[theCellWithTheTextFieldToClear viewWithTag:11];

You can find more information about tags in the UIView Class Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

Good Luck

EDIT 2:

If you are using two tables, and you want to clear a textfield in one table, selecting the other one, i think that you need something to make the link between the indexPath selected in one table and the indexPath for the cell with the textfield. If you are using something like:

table 1, indexpath:0,0 for celcius; table 1, indexpath:0,1 for farenheit; table 1, indexpath:0,2 for kelvin;

and

table 2, indexpath:0,0 for celcius; table 2, indexpath:0,1 for farenheit; table 2, indexpath:0,2 for kelvin;

You just can use the same indexPath to get the cell with the textfield FROM THE OTHER TABLEVIEW (Inputtableview) like:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cellWithTheFieldToClear = [yourInputTableView cellForRowAtIndexPath:indexPath];
    theTextFieldToClear = (UITextField*)[theCellWithTheTextFieldToClear viewWithTag:2];
    theTextFieldToClear.text = @"";
}

Upvotes: 1

John
John

Reputation: 2694

Did you add your myAccessToMaintxtf as subview of the UITableViewCell in dataSource cellForRowAtIndexPath? If you did, you still have to reload the cell using [tableView reloadCellAtIndexPath:...];

Or another suggestion is to subclass the UITableViewCell, and add an Outlet property, connect the outlet to the TextField. Then you can do something like selectedCell.textField.text = @""; in you didSelectRowAtIndexPath.

Upvotes: 1

Related Questions