-1

In my application i am having 37 textfield on tableview to scroll.If suppose i enter some value in first textfield and scroll the table ,the value enter in first textfiled disappaears as well as the layout.can any one help me out;

following is my code:

#import "Edit.h"


@implementation Edit
@synthesize tableContents,textField,scrollView;
@synthesize txtUserName,txtFirstName,txtLastName,txtNickName,txtDisplayName,txtEmail,txtWebSite,txtAboutMe,txtNewPassword,txtPasswordAgain;
@synthesize txtPayPalEmail ,txtActiveMemPk ,txtMemPkExpireDate;
@synthesize txtBusinessName ,txtAbnAcn ,txtContactName ,txtPhone ,txtFax ,txtMobile ,txtBusinessEmail ,txtFacebookLink ,txtLinkedinLink ,txtMySpaceLink;
@synthesize txtBlogLink ,txtInstanMessage ,txtWebsite ,txtStreet ,txtCitySuburb ,txtZipCode ,txtState ,txtTradingHour;
@synthesize  txtActiveOfService ,txtTradeOnWeekend ,txtProduct , txtService ,txtPickUpAndDelivery;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}



#pragma mark - View lifecycle

- (void)viewDidLoad
{

    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"main-bg.png"]];

    UIBarButtonItem *updatebtn = [[UIBarButtonItem alloc] 
                                  initWithTitle:@"Update"                                            
                                  style:UIBarButtonItemStyleBordered 
                                  target:self 
                                  action:@selector(update)];
    self.navigationItem.rightBarButtonItem = updatebtn;

    [updatebtn release];

    [super viewDidLoad];
    NSArray *text = [[NSArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
    NSDictionary *textData = [[NSDictionary alloc]initWithObjectsAndKeys:text,@"",nil];
    self.tableContents = textData;
    self.textField = [[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)];



}




-(NSInteger) numberOfSectionInTableView:(UITableView *)tableView
{
    return[self.textField count];
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSArray *lsData =[self.tableContents objectForKey:[self.textField objectAtIndex:section]];
    return [lsData count];

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *TextCellIdentifier =@"Cell";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];


    if (cell==nil)
    {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier ] autorelease];
        cell.accessoryType= UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.font = [UIFont fontWithName:@"Helvetica" size:14];
    }
    if ([indexPath row]==0) 
    {
        txtUserName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtUserName.placeholder=@"username";
        txtUserName.autocorrectionType=UITextAutocorrectionTypeNo;
        [txtUserName setClearButtonMode:UITextFieldViewModeWhileEditing];
        [txtUserName setEnabled:YES];
        cell.accessoryView=txtUserName;
        cell.textLabel.text=@"User Name:";


    }
    if ([indexPath row]==1) 
    {
        txtFirstName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtFirstName.placeholder=@"first name";
        cell.accessoryView=txtFirstName;
        cell.textLabel.text=@"First Name:";
    }
    if ([indexPath row]==2) 
    {
        txtLastName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtLastName.placeholder=@"last name";
        cell.accessoryView=txtLastName;
        cell.textLabel.text=@"Last Name";
    }
    if ([indexPath row]==3) 
    {
        txtNickName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtNickName.placeholder=@"nick name";
        cell.accessoryView=txtNickName;
        cell.textLabel.text=@"Nick Name";
    }
    if ([indexPath row]==4) 
    {
        txtDisplayName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtDisplayName.placeholder=@"display name";
        cell.accessoryView=txtDisplayName;
        cell.textLabel.text=@"Diaplay Name:";
    }
    if ([indexPath row]==5) 
    {
        txtEmail=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtEmail.placeholder=@"email";
        cell.accessoryView=txtEmail;
        cell.textLabel.text=@"Email ID:";
        //txtEmail.text=[NSString stringWithFormat:@"%@",user_email];
        //cell.textAlignment=UITextAlignmentRight;  
    }
    if ([indexPath row]==6) 
    {
        txtWebSite=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtWebSite.placeholder=@"website";
        cell.accessoryView=txtWebSite;
        cell.textLabel.text=@"Website:";
    }
    if ([indexPath row]==7) 
    {
        txtAboutMe=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        cell.accessoryView=txtAboutMe;
        cell.textLabel.text=@"About Me:";
        //cell.font =[UIFont fontWithName:@"impact" size:14];
        //txtAboutMe.font=[UIFont boldSystemFontOfSize:20];
        //txtAboutMe.backgroundColor=[UIColor clearColor];
        //cell.textAlignment=UITextAlignmentRight;
    }
    if ([indexPath row]==8) 
    {
        txtNewPassword=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtNewPassword.placeholder=@"new password";
        cell.accessoryView=txtNewPassword;
        cell.textLabel.text=@"New Password:";
    }
    if ([indexPath row]==9) 
    {
        txtPasswordAgain=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtPasswordAgain.placeholder=@"confirm password";
        cell.accessoryView=txtPasswordAgain;
        cell.textLabel.text=@"Confirm Password:";
    }
    if ([indexPath row]==10) 
    {
        UILabel *lblExtendedProfileInfo=[[UILabel alloc] initWithFrame:CGRectMake(0,0,270,25)];
        lblExtendedProfileInfo.text=@"Extended Profile Information:";
        lblExtendedProfileInfo.font=[UIFont boldSystemFontOfSize:14];
        cell.accessoryView=lblExtendedProfileInfo;
        lblExtendedProfileInfo.textAlignment=UITextAlignmentLeft;
    }
    if ([indexPath row]==11) 
    {
        txtPayPalEmail=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtPayPalEmail.placeholder=@"pay pal email";
        cell.accessoryView=txtPayPalEmail;
        cell.textLabel.text=@"Pay Pal Email";
    }
    if ([indexPath row]==12) 
    {
        txtActiveMemPk=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtActiveMemPk.placeholder=@"Active Member Pack";
        cell.accessoryView=txtActiveMemPk;
        cell.textLabel.text=@"Active Memberhip:";
    }
    if ([indexPath row]==13) 
    {
        txtMemPkExpireDate=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtMemPkExpireDate.placeholder=@"member pack expire date";
        cell.accessoryView=txtMemPkExpireDate;
        cell.textLabel.text=@"Membership Pack:";
    }
    if ([indexPath row]==14) 
    {
        UILabel *lblExtendedProfileInfo=[[UILabel alloc] initWithFrame:CGRectMake(0,0,270,25)];
        lblExtendedProfileInfo.text=@"Business Information:";
        lblExtendedProfileInfo.font=[UIFont boldSystemFontOfSize:14];
        cell.accessoryView=lblExtendedProfileInfo;
        //lblExtendedProfileInfo.textAlignment=UITextAlignmentLeft;

    }
    if ([indexPath row]==15) 
    {
        txtBusinessName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        txtBusinessName.placeholder=@"Business name";
        cell.accessoryView=txtBusinessName;
        cell.textLabel.text=@"Business Name:";
    }
    if ([indexPath row]==16) 
    {
        txtAbnAcn=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtAbnAcn.placeholder=@" Abn /Acn";
        cell.accessoryView=txtAbnAcn;
        cell.textLabel.text=@"Abn/Acn:";
    }
    else if ([indexPath row]==17)
    {
        txtContactName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtContactName.placeholder=@"Contact Name";
        cell.accessoryView=txtContactName;
        cell.textLabel.text=@"Contact Name:";

    }


    else if ([indexPath row]==18)
    {
        txtPhone=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtPhone.placeholder=@"Phone No";
        cell.accessoryView=txtPhone;
        cell.textLabel.text=@"Phone No:";

    }

    else if ([indexPath row]==19)
    {
        txtFax=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtFax.placeholder=@"Fax ";
        cell.accessoryView=txtFax;
        cell.textLabel.text=@"Fax:";
    }

    else if ([indexPath row]==20)
    {
        txtMobile=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtMobile.placeholder=@"Mobile No";
        cell.accessoryView=txtMobile;
        cell.textLabel.text=@"Mobile No:";

    }

    else if ([indexPath row]==21)
    {
        txtBusinessEmail=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtBusinessEmail.placeholder=@" Business Email";
        cell.accessoryView=txtBusinessEmail;
        cell.textLabel.text=@"Business Email:";


    }

    else if ([indexPath row]==22)
    {
        txtFacebookLink=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtFacebookLink.placeholder=@"Facebook Link";
        cell.accessoryView=txtFacebookLink;
        cell.textLabel.text=@"Facebook Link:";


    }

    else if ([indexPath row]==23)
    {
        txtLinkedinLink=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtLinkedinLink.placeholder=@"Linked In Link";
        cell.accessoryView=txtLinkedinLink;
        cell.textLabel.text=@"Linked In Link:";

    }

    else if ([indexPath row]==24)
    {
        txtMySpaceLink=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtMySpaceLink.placeholder=@"My Space Link";
        cell.accessoryView=txtMySpaceLink;
        cell.textLabel.text=@"My Space Link:";
    }

    else if ([indexPath row]==25)
    {
        txtBlogLink=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtBlogLink.placeholder=@"Blog Link";
        cell.accessoryView=txtBlogLink;
        cell.textLabel.text=@"Blog Link:";

    }

    else if ([indexPath row]==26)
    {
        txtInstanMessage=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtInstanMessage.placeholder=@"Instant Message";
        cell.accessoryView=txtInstanMessage;
        cell.textLabel.text=@"Instant Message:";

    }
    else if ([indexPath row]==27)
    {
        txtWebsite=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtWebsite.placeholder=@"Web Site";
        cell.accessoryView=txtWebsite;
        cell.textLabel.text=@"Web Site:";

    }

    else if ([indexPath row]==28)
    {
        txtStreet=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtStreet.placeholder=@"Street";
        cell.accessoryView=txtStreet;
        cell.textLabel.text=@"Street:";

    }

    else if ([indexPath row]==29)
    {
        txtCitySuburb=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtCitySuburb.placeholder=@"City Suburb";
        cell.accessoryView=txtCitySuburb;
        cell.textLabel.text=@"City Suburb:";

    }

    else if ([indexPath row]==30)
    {
        txtZipCode=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtZipCode.placeholder=@"Zip Code";
        cell.accessoryView=txtZipCode;
        cell.textLabel.text=@"Zip Code:";

    }
    else if ([indexPath row]==31)
    {

        cell.textLabel.text=@"State:";
        cell.textAlignment=UITextAlignmentRight;

        //arrayData = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"Vinod",@"Parth",@"Nilesh",nil]];
//      
//      stateDropDownBox = [[DropDownView alloc] initWithArrayData:arrayData cellHeight:30 heightTableView:100 paddingTop:-8 paddingLeft:-5 paddingRight:-10 refView:stateDDownBox animation:BLENDIN openAnimationDuration:2 closeAnimationDuration:2];
//      
//      stateDropDownBox.delegate = self;
//      [self.view addSubview:stateDropDownBox.view];
//      
//      stateDDownBox=[UIButton buttonWithType:UIButtonTypeCustom];
//      //ddState.frame=CGRectMake(0, 0,360,45);
//      stateDDownBox.frame=CGRectMake(0, 0,320,45);
//      UIImage *img=[UIImage imageNamed:@"drop-down-box.png"];
//      [stateDDownBox setBackgroundImage:img forState:UIControlStateNormal];
//      [stateDDownBox addTarget:self action:@selector(dropDownClick) forControlEvents:UIControlEventTouchUpInside];
//      [stateDDownBox setTitle:[arrayData objectAtIndex:0] forState:UIControlStateNormal];
//      cell.accessoryView=stateDDownBox;


    }

    else if ([indexPath row]==32)
    {
        txtTradingHour=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtTradingHour.placeholder=@"Trading Hour";
        cell.accessoryView=txtTradingHour;
        cell.textLabel.text=@"Trading Hour:";
    }

    if ([indexPath row]==33)
    {
        txtActiveOfService=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtActiveOfService.placeholder=@"Active Of Service";
        cell.accessoryView=txtActiveOfService;
        cell.textLabel.text=@"Active Of Service:";

    }

    else if ([indexPath row]==34)
    {
        txtTradeOnWeekend=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtTradeOnWeekend.placeholder=@"Trade Of Weekend";
        cell.accessoryView=txtTradeOnWeekend;
        cell.textLabel.text=@"Do you trade on Weekends?:";
        //      button=[UIButton buttonWithType:UIButtonTypeCustom];
        //      button.frame=CGRectMake(0, 0,360,45);
        //      UIImage *img=[UIImage imageNamed:@"button_back.png"];
        //      [button setBackgroundImage:img forState:UIControlStateNormal];
        //      [button addTarget:self action:@selector(actionButtonClick) forControlEvents:UIControlEventTouchUpInside];
        //      [button setTitle:[arrayData objectAtIndex:0] forState:UIControlStateNormal];
        //      cell.accessoryView=button;

    }

    else if ([indexPath row]==35)
    {
        txtProduct=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtProduct.placeholder=@"Product";
        cell.accessoryView=txtProduct;
        cell.textLabel.text=@"Product:";
        //txtProduct.text=[NSString stringWithFormat:@"%@",business_product];
        //cell.textAlignment=UITextAlignmentRight;
    }

    else if ([indexPath row]==36)
    {
        txtPickUpAndDelivery=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
        txtPickUpAndDelivery.placeholder=@"Pickup and Delivery";
        cell.accessoryView=txtPickUpAndDelivery;
        cell.textLabel.text=@"Pickup and Delivery:";
        //txtPickUpAndDelivery.text=[NSString stringWithFormat:@"%@",business_delivery];
        //cell.textAlignment=UITextAlignmentRight;


    }




        return cell;

}

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];


}

//-(BOOL)textFieldShouldReturn:(UITextField *)textField {
//    [textField resignFirstResponder];
//    return YES;
//}

@end

3 Answers3

0

This may be due to your deQueue property of the cell,

You try by creating cell like this

UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TextCellIdentifier] autorelease];
cell.accessoryType= UITableViewCellAccessoryNone;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.font = [UIFont fontWithName:@"Helvetica" size:14];
EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
0

Managing textfields in Tableview is a bit tediuos, If the number of textfields are fixed you can try using view controller with 37 (If the number is fixed) textfield in a scroll view. Manging the scrolling can be done using this

If you still want to stick to your appraoch. then keeping the values of textfields in a mutable dictionary and use the data when you scroll through the tableview.

Edit: The screen shot attached shows a simple view holding scrollview with multiple textfields. You can create IBOutlets to link these textfield in your code and set/get the values in these. If the number of textfields are too many you can use the above mentioned approach to scroll down to the last textfield and manage keyboard covering up the view.

enter image description here

Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47
  • can you plz suggest ...how to set 35 textfield in a uivew – Nilesh Joshi Nov 29 '11 at 07:00
  • Take UIScrollView and add the desired textfields using interfacebuilder. Using Interface builder will speed up your task. See my updated answer. – Rahul Sharma Nov 29 '11 at 07:09
  • you can use http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html or simply drag and drop textviews over the View and they will fall in a tree fashion as shown in screenshot. – Rahul Sharma Nov 29 '11 at 10:23
0

you need to uitextfieldDelegate in .h file

in .m file

- (void)textFieldDidEndEditing:(UITextField *)textField {
if ( textField == txtUserName ) {
    self.name = textField.text ;
} else if ( textField == txtNickName ) {
    self.address = textField.text ;
} else if ( textField == txtDisplayName ) {
    self.password = textField.text ;
} else if ( textField == txtEmail ) {
    self.description = textField.text ;     
}

}

Priyanka Singh
  • 250
  • 2
  • 2