滚动时如何设置文本字段内容?

时间:2011-11-29 05:11:43

标签: iphone objective-c ios xcode xcode3.2

在我的应用程序中,我在tableview上有37个textfield滚动。如果我在第一个文本字段中输入一些值并滚动表格,则输入第一个textfiled disappaears以及layout.can中的任何一个帮助我的值;

以下是我的代码:

#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 个答案:

答案 0 :(得分:0)

这可能是由于您的deQueue属性,

您尝试创建像这样的单元格

UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TextCellIdentifier] autorelease];
cell.accessoryType= UITableViewCellAccessoryNone;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.font = [UIFont fontWithName:@"Helvetica" size:14];

答案 1 :(得分:0)

在Tableview中管理文本字段有点tediuos,如果修复了文本字段的数量,您可以尝试在滚动视图中使用带有37的视图控制器(如果数字是固定的)文本字段。可以使用this

完成滚动管理

如果你还想坚持你的appraoch。然后将文本字段的值保存在可变字典中,并在滚动查看tableview时使用数据。

编辑:附加的屏幕截图显示了一个包含多个文本字段的滚动视图的简单视图。您可以创建IBOutlets以在代码中链接这些文本字段,并设置/获取这些中的值。如果文本字段的数量太多,您可以使用上述方法向下滚动到最后一个文本字段并管理覆盖视图的键盘。

enter image description here

答案 2 :(得分:0)

你需要在.h文件中使用uitextfieldDelegate

<。>文件中的

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

}