How to put a UITextField inside of a UITableViewCell (grouped)? I want a user to be able to edit it.
Add the UITextField as an subview of the UITableViewCell's contentView:
[mycell.contentView addSubview:view];
Apple's own UICatalog demo application has an example of placing UITextFields in Grouped UITableView Cells: http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html
Check the contents of TextFieldController.m
Plus there's lots of other great code there for working with UIKit Objects.
This is how I've implemented it into my application, however you'll obviously need to change a few things. Hope this helps.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.
if ([indexPath section] == 0) {
tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tUser.adjustsFontSizeToFitWidth = YES;
tUser.textColor = [UIColor blackColor];
tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tPass.adjustsFontSizeToFitWidth = YES;
tPass.textColor = [UIColor blackColor];
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
tUser.placeholder = #"#JohnAppleseed";
tUser.keyboardType = UIKeyboardTypeEmailAddress;
tUser.returnKeyType = UIReturnKeyNext;
}
if ([indexPath row] == 1) {
tPass.placeholder = #"Required";
tPass.keyboardType = UIKeyboardTypeDefault;
tPass.returnKeyType = UIReturnKeyDone;
tPass.secureTextEntry = YES;
}
}
tUser.backgroundColor = [UIColor whiteColor];
tUser.autocorrectionType = UITextAutocorrectionTypeNo;
tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
tUser.textAlignment = UITextAlignmentLeft;
tPass.backgroundColor = [UIColor whiteColor];
tPass.autocorrectionType = UITextAutocorrectionTypeNo;
tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
tPass.textAlignment = UITextAlignmentLeft;
tUser.clearButtonMode = UITextFieldViewModeNever;
tPass.clearButtonMode = UITextFieldViewModeNever;
[tUser setEnabled:YES];
[tPass setEnabled:YES];
//[tUser release];
//[tPass release];
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = #"Username";
[cell addSubview:tUser];
[tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_name_preference"]];
}
else {
cell.textLabel.text = #"Password";
[cell addSubview:tPass];
[tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_pass_preference"]];
}
}
return cell; }
Hope it helps.
Related
I have a UITableView that I used for username/password enter.
But I have problem to get values when I click on button.
This is how I make table:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = #"example#gmail.com";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
}
else {
playerTextField.placeholder = #"Required";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = NSTextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
playerTextField.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:playerTextField];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = #"Email";
}
else {
cell.textLabel.text = #"Password";
}
}
return cell;
}
And this is what I have tried in button tap:
...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
UITextField* aField = (UITextField *)[aCell viewWithTag:0];
NSLog(aField.text);
}
..
This prints me just: Email Password (labels not data that I entered).
How to get entered values here?
All views default their tag to 0. Because of this, [aCell viewWithTag: 0] is returning the first view with tag zero - in this case the cell's textLabel - which is a UILabel that also responds to .text. If you set the tags in your cellForRowAtIndexPath: to something other than zero it should start working for you.
In that case you can give the tag to textfield like playerTextField.tag = 115 and you can get view by tag like UITextField *aField = (UITextField *)[aCell viewWithTag:115]; and display NSLog(#"text:%#",aField.text);
So, I am creating a login screen with UITableView. There are two textfields for email and password. When the user presses 'Login' button, I want to store the contents of those two textfields in two NSStrings. How do I do that? Here is the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITextField *tf = [[UITextField alloc] init];
UITableViewCell *cell = [self.tView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = #"Email";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
tf = jidField = [self makeTextField:#"" placeholder:playerTextField.placeholder];
[cell addSubview:jidField];
}
else {
playerTextField.placeholder = #"Password";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
//playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
playerTextField.textAlignment = NSTextAlignmentLeft;
// playerTextField.tag = 0;
//playerTextField.delegate = self;
playerTextField.clearButtonMode = YES; // no clear 'x' button to the right
[playerTextField setEnabled: YES];
[cell addSubview:playerTextField];
}
}
return cell;
}
In cellForRowAtIndexPath, set tag for userid and password field and retrieve the textfields with its tag identifier. Please find following implementation of loginAction method.
- (void)loginAction:(id)sender {
NSIndexPath *userIdRow = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *passwordFieldRow = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *userIdTableCell = (UITableViewCell *)[tableview cellForRowAtIndexPath:userIdRow];
UITableViewCell *passwordFieldTableCell = (UITableViewCell *)[tableview cellForRowAtIndexPath:userIdRow];
UITextField *userIDField = (UITextField *)[userIdTableCell viewWithTag:1001];
UITextField *passwordField = (UITextField *)[passwordFieldTableCell viewWithTag:1002];
// You can store now text to either your instance variables or properties in the following statements
NSLog(#"%#", userIDField.text];
NSLog(#"%#", passwordField.text];
}
In cellForRowAtIndexPath, you have to tag your's textfields
if ([indexPath row] == 0) {
playerTextField.tag = 1001;
} else {
playerTextField.tag = 1002;
}
Just like Tarek said, this is how you do it.
In your *.h file
#interface v1ViewController : UITableViewController
{
UITextField IBOutlet *playerEmailTxt;
UITextField IBOutlet *playerPassword;
}
#property (nonatomic, retain) UITextField IBOutlet *playerEmailTxt;
#property (nonatomic, retain) UITextField IBOutlet *playerPassword;
In your *.m file
...
#synthesize playerEmailTxt;
#synthesize playerPassword;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITableViewCell *cell = [self.tView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 1)
{
if (indexPath.row == 0)
{
playerEmailTxt = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
playerEmailTxt.adjustsFontSizeToFitWidth = YES;
playerEmailTxt.textColor = [UIColor blackColor];
playerEmailTxt.placeholder = #"Email";
playerEmailTxt.keyboardType = UIKeyboardTypeEmailAddress;
playerEmailTxt.returnKeyType = UIReturnKeyNext;
[cell addSubview:playerEmailTxt];
}
else if (indexPath.row == 1)
{
playerPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
playerPassword.adjustsFontSizeToFitWidth = YES;
playerPassword.textColor = [UIColor blackColor];
playerPassword.placeholder = #"Password";
playerPassword.keyboardType = UIKeyboardTypeDefault;
playerPassword.returnKeyType = UIReturnKeyDone;
playerPassword.secureTextEntry = YES;
[cell addSubview:playerPassword];
}
}
return cell;
}
Now add your login action
-(IBAction)LoginAction
{
NSMutableString *usrEmailStr = [NSMutableString stringWithFormat:#"%#", playerEmailTxt];
NSMutableString *usrPasswdStr = [NSMutableString stringWithFormat:#"%#", playerPassword];
//Do whatever you want with these strings
}
I'd also recommend you check out the free Sensible TableView framework. Seems like a perfect fit for what you're trying to do, since the framework will automatically fetch the data from your fields and store them in any data structure you wish.
I am doing one registration form for iphone application. In that am having one tableview with four section.In last section am having one row with register button.I got the tableview with data well.But while scrolling the tableview up and down my register button goes to 2nd section.I added the code below.What wrong i did in this.Help me to solve.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *identifier = #"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSLog(#" row and section ===== %d,%d",indexPath.row,indexPath.section);
if(indexPath.section != 3)
{
UILabel *lbl_title = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(7,10,250,50):CGRectMake(70,40,340, 35))];
[lbl_title setFont:[UIFont fontWithName:#"Verdana-Bold" size:(isiPad?20:13)]];
lbl_title.backgroundColor = [UIColor clearColor];
[lbl_title setTextColor:[UIColor blackColor]];
lbl_title.tag = ((indexPath.section+1)*10)+indexPath.row;
//NSLog(#"lbl data tag %d",lbl_title.tag);
lbl_title.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:lbl_title];
UILabel *lbl_data = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(260,10,410,50):CGRectMake(70,40,340, 35))];
[lbl_data setFont:[UIFont fontWithName:#"Verdana-Bold" size:(isiPad?20:13)]];
lbl_data.backgroundColor = [UIColor clearColor];//25-25-112
[lbl_data setTextColor:[UIColor colorWithRed:25.0/255.0 green:25.0/255.0 blue:112.0/255.0 alpha:1.0]];
lbl_data.textAlignment = UITextAlignmentCenter;
lbl_data.tag = ((indexPath.section+1)*100)+indexPath.row;
//NSLog(#"lbl data tag %d,%#",lbl_data.tag,lbl_data);
[cell.contentView addSubview:lbl_data];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
lbl_title.text = #"Email";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"Password";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Confirm password";
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
lbl_title.text = #"First name";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"Last name";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Nickname";
}
else if(indexPath.row == 3)
{
lbl_title.text = #"Gender";
}
}
else if(indexPath.section == 2)
{
if(indexPath.row == 0)
{
lbl_title.text = #"City";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"State";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Country";
}
}
}
if(indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.backgroundColor = [UIColor colorWithRed:165.0/255.0 green:42.0/255.0 blue:42.0/255.0 alpha:1.0];//165-42-42
UIButton *btn_register = [[UIButton alloc] initWithFrame:isiPad?CGRectMake(10,0,660,70):CGRectMake(0,0,320,40)];
[btn_register addTarget:self action:#selector(btn_register_clicked:) forControlEvents:UIControlEventTouchUpInside];
[btn_register setTitle:#"Register" forState:UIControlStateNormal];
//btn_register.titleLabel.textAlignment = UITextAlignmentCenter;
btn_register.backgroundColor = [UIColor blueColor];
[btn_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn_register setFont:[UIFont fontWithName:#"Verdana-Bold" size:24]];
[cell.contentView addSubview:btn_register];
}
}
}
else
{
NSLog(#"updating cell.........");
}
return cell;
}
It gets repeated because you added it to a cell's contentview, and then you re-use that cell.
Rather than adding it in cell's contentView, add the button as a cell accessory.
cell.accessoryView = btn_register;
And in all other cells, set accessoryView to nil.
cell.accessoryView = nil;
Why don't you try setting different identifiers for each section of cells. You can also use Use [tableView dequeueReusableCellWithIdentifier:forIndexPath:
Using a different cell identifier for the "Register" cell should work. And your code will be much clear if you use a container to hold the data, like an NSDictionary. For example:
NSArray *account = #[#"Email", #"Password", #"Confirm password"];
NSArray *userInfo = #[#"First name", #"Last name", #"Gender", #"Nickname"];
// ...
NSDictionary *data = #{#"Account": account, #"User Information": userInfo /* ... */};
In my app i am customised the UITableView Cell with four UITextViews. Whenever i added data to the tableview and reload it. The text in the UITableViewCell get override with the previous texts.
I tried different approaches but couldn't figure out what was the problem.
I am using TableView in View Controller.
Here is the Code i used in my table View cell?
if ( [tableView isEqual:self.tableActions])
{
//Setting the text empty
static NSString *CellIdentifier = #"ActionsIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
NSLog(#"Index Path %i",[indexPath row]);
ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];
NSLog(#"Action Text %#",actiondetails.actions);
//Actions
actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
actionText.font = [UIFont systemFontOfSize:17.0];
actionText.editable = NO;
actionText.textColor=[UIColor blackColor];
actionText.text = actiondetails.actions ;
actionText.userInteractionEnabled = NO;
actionText.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:actionText];
//Owner
ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
ownerBy.font = [UIFont systemFontOfSize:17.0];
ownerBy.textColor=[UIColor blackColor];
ownerBy.textAlignment = UITextAlignmentCenter;
ownerBy.text = actiondetails.owner;
ownerBy.editable = NO;
ownerBy.userInteractionEnabled = NO;
ownerBy.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:ownerBy];
}
ScreenShot
Thanks for your help guys.
Much Appreciated.
This happened to me a other day, the solution that I came up with was removing all subview from the cell after creating the cell in the if statement.
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
Remove cell identifier of Tableview otherwise take Customcell for the Tableview..
Just simple try this way :
{
//Setting the text empty
static NSString *CellIdentifier = #"ActionsIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
**// if(cell == nil) // comment this line in your code ,its work
// {**
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
**// }**
NSLog(#"Index Path %i",[indexPath row]);
ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];
NSLog(#"Action Text %#",actiondetails.actions);
//Actions
actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
actionText.font = [UIFont systemFontOfSize:17.0];
actionText.editable = NO;
actionText.textColor=[UIColor blackColor];
actionText.text = actiondetails.actions ;
actionText.userInteractionEnabled = NO;
actionText.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:actionText];
//Owner
ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
ownerBy.font = [UIFont systemFontOfSize:17.0];
ownerBy.textColor=[UIColor blackColor];
ownerBy.textAlignment = UITextAlignmentCenter;
ownerBy.text = actiondetails.owner;
ownerBy.editable = NO;
ownerBy.userInteractionEnabled = NO;
ownerBy.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:ownerBy];
}
You are reusing the cell. Reused cell already has UITextview added to it.So you are overriding on it.
You must shift all your code of creating and adding UITextView to.
If(cell==nil)
{
}
After which you only need to set the Text to UITextview.
when your view did appear write reload your table view.
-(void)viewDidAppear:(BOOL)animated {
[tableView reloadData];
}
change your cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// your code
}
// your code
return cell;
}
for (id vw in [[cell contentView] subviews]) {
if ([vw isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)vw;
[label removeFromSuperview];
}
}
for (id vw in [cell subviews]) {
if ([vw isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)vw;
[label removeFromSuperview];
}
}
When I am entering text in textfield 1 that text is also displayed in textfield 9.
here is some code what i did to create textfield in tableview cell.
i added textfield in tableview cell .like below in cellforRowatintexpath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCell";
//UILabel* nameLabel = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text = [labels objectAtIndex:indexPath.row];
nameLabel.font = [UIFont boldSystemFontOfSize:12];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.tag =1;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
[nameLabel release];
//adding textfield to tableview cell.
tv = [[UITextField alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+2, 10.0, cell.contentView.bounds.size.width, 30)];
tv.tag = indexPath.row + 2;
//tv.text =[labels objectAtIndex:indexPath.row];
tv.font = [UIFont boldSystemFontOfSize:12];
//this method will take text from textfield of tableview cell using their individual tag
[tv addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:tv];
[tv setDelegate:self];
[tv release];
return cell;}
and here is the method getTextFieldValue method
- (void) getTextFieldValue:(UITextField *)textField{
if (textField.tag == 2) {
//NSString *value1 = [NSString stringWithFormat:#"%#",textField.text];
//NSLog(#"value1 is %#",value1);.
NSLog(#"2--->%#",textField.text);
}else if(textField.tag == 3){
NSLog(#"3--->%#",textField.text);
}else if(textField.tag == 4){
NSLog(#"4--->%#",textField.text);
}
else if(textField.tag == 5){
NSLog(#"5--->%#",textField.text);
}
else if(textField.tag == 6){
NSLog(#"6--->%#",textField.text);
}
else if(textField.tag == 7){
NSLog(#"7--->%#",textField.text);
}
else if(textField.tag == 8){
NSLog(#"8--->%#",textField.text);
}
else if(textField.tag == 9){
NSLog(#"9--->%#",textField.text);
}
else if(textField.tag == 10){
NSLog(#"10--->%#",textField.text);
}
else if(textField.tag == 11){
NSLog(#"11--->%#",textField.text);
}
else if(textField.tag == 12){
NSLog(#"12--->%#",textField.text);
} }
First of all, please rewrite your getTextFieldValue method like this:
- (void)getTextFieldValue:(UITextField *)textField {
NSLog(#"%d--->%#", textField.tag, textField.text);
}
You keep adding UITextFields to the cell, even when it's reused. Try putting that code inside the if statement.
You should set the text of the text field to nil every time though. Else it will still be there on dequeued cells (when scrolling).
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCell";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
//TODO: Set up nameLabel here
[cell.contentView addSubview: nameLabel];
[nameLabel release];
UITextField *tv = [[UITextField alloc] init];
tv.font = [UIFont boldSystemFontOfSize:12];
[tv setDelegate:self];
[tv addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:tv];
[tv release];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [labels objectAtIndex:indexPath.row];
[nameLabel sizeToFit];
return cell;
}
That is the retain problem is there.
You can take different cellIdentifier for each row. Only change one statement you get your solution.
Solve this problem with above method. i was facing same problem & i solve with this method.
I hope this will help you.
NSString *CellIdentifier = [NSString stringWithFormat:#"cellidentifier-%d",indexPath.row];
Maybe you have set some IBOutlet to the textField? Did you?