2011-11-29 45 views
-1

在我的應用程序中,我在tableview上有37個textfield進行滾動。如果假設我在第一個文本框中輸入了一些值並滾動了表格,那麼在第一個文本文件中輸入的值以及layout可以幫助我輸入值。如何在滾動時設置textfield內容?

下面是我的代碼:

#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 [email protected]"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)]; 
     [email protected]"username"; 
     txtUserName.autocorrectionType=UITextAutocorrectionTypeNo; 
     [txtUserName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
     [txtUserName setEnabled:YES]; 
     cell.accessoryView=txtUserName; 
     [email protected]"User Name:"; 


    } 
    if ([indexPath row]==1) 
    { 
     txtFirstName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"first name"; 
     cell.accessoryView=txtFirstName; 
     [email protected]"First Name:"; 
    } 
    if ([indexPath row]==2) 
    { 
     txtLastName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"last name"; 
     cell.accessoryView=txtLastName; 
     [email protected]"Last Name"; 
    } 
    if ([indexPath row]==3) 
    { 
     txtNickName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"nick name"; 
     cell.accessoryView=txtNickName; 
     [email protected]"Nick Name"; 
    } 
    if ([indexPath row]==4) 
    { 
     txtDisplayName=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"display name"; 
     cell.accessoryView=txtDisplayName; 
     [email protected]"Diaplay Name:"; 
    } 
    if ([indexPath row]==5) 
    { 
     txtEmail=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"email"; 
     cell.accessoryView=txtEmail; 
     [email protected]"Email ID:"; 
     //txtEmail.text=[NSString stringWithFormat:@"%@",user_email]; 
     //cell.textAlignment=UITextAlignmentRight; 
    } 
    if ([indexPath row]==6) 
    { 
     txtWebSite=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"website"; 
     cell.accessoryView=txtWebSite; 
     [email protected]"Website:"; 
    } 
    if ([indexPath row]==7) 
    { 
     txtAboutMe=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     cell.accessoryView=txtAboutMe; 
     [email protected]"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)]; 
     [email protected]"new password"; 
     cell.accessoryView=txtNewPassword; 
     [email protected]"New Password:"; 
    } 
    if ([indexPath row]==9) 
    { 
     txtPasswordAgain=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"confirm password"; 
     cell.accessoryView=txtPasswordAgain; 
     [email protected]"Confirm Password:"; 
    } 
    if ([indexPath row]==10) 
    { 
     UILabel *lblExtendedProfileInfo=[[UILabel alloc] initWithFrame:CGRectMake(0,0,270,25)]; 
     [email protected]"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)]; 
     [email protected]"pay pal email"; 
     cell.accessoryView=txtPayPalEmail; 
     [email protected]"Pay Pal Email"; 
    } 
    if ([indexPath row]==12) 
    { 
     txtActiveMemPk=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"Active Member Pack"; 
     cell.accessoryView=txtActiveMemPk; 
     [email protected]"Active Memberhip:"; 
    } 
    if ([indexPath row]==13) 
    { 
     txtMemPkExpireDate=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 25)]; 
     [email protected]"member pack expire date"; 
     cell.accessoryView=txtMemPkExpireDate; 
     [email protected]"Membership Pack:"; 
    } 
    if ([indexPath row]==14) 
    { 
     UILabel *lblExtendedProfileInfo=[[UILabel alloc] initWithFrame:CGRectMake(0,0,270,25)]; 
     [email protected]"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)]; 
     [email protected]"Business name"; 
     cell.accessoryView=txtBusinessName; 
     [email protected]"Business Name:"; 
    } 
    if ([indexPath row]==16) 
    { 
     txtAbnAcn=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)]; 
     [email protected]" Abn /Acn"; 
     cell.accessoryView=txtAbnAcn; 
     [email protected]"Abn/Acn:"; 
    } 
    else if ([indexPath row]==17) 
    { 
     txtContactName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)]; 
     [email protected]"Contact Name"; 
     cell.accessoryView=txtContactName; 
     [email protected]"Contact Name:"; 

    } 


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

    } 

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

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

    } 

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


    } 

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


    } 

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

    } 

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

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

    } 

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

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

    } 

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

    } 

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

    } 

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

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

     [email protected]"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)]; 
     [email protected]"Trading Hour"; 
     cell.accessoryView=txtTradingHour; 
     [email protected]"Trading Hour:"; 
    } 

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

    } 

    else if ([indexPath row]==34) 
    { 
     txtTradeOnWeekend=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)]; 
     [email protected]"Trade Of Weekend"; 
     cell.accessoryView=txtTradeOnWeekend; 
     [email protected]"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)]; 
     [email protected]"Product"; 
     cell.accessoryView=txtProduct; 
     [email protected]"Product:"; 
     //txtProduct.text=[NSString stringWithFormat:@"%@",business_product]; 
     //cell.textAlignment=UITextAlignmentRight; 
    } 

    else if ([indexPath row]==36) 
    { 
     txtPickUpAndDelivery=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)]; 
     [email protected]"Pickup and Delivery"; 
     cell.accessoryView=txtPickUpAndDelivery; 
     [email protected]"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 

回答

0

在泰伯維管理文本框創建細胞有點tediuos,如果文本字段的數量是固定的,你可以嘗試使用視圖控制器與37(如果數量是固定的)textfield在滾動視圖。滾動可以通過使用this

如果你仍然想堅持你的想法。然後將textfields的值保存在可變字典中,並在滾動tableview時使用這些數據。

編輯:附加的屏幕快照顯示了一個簡單的視圖,其中包含多個文本框的滾動視圖。您可以創建IBOutlets以將代碼中的這些文本字段鏈接起來,並設置/獲取這些值。如果文本字段的數量太多,則可以使用上述方法向下滾動到最後一個文本字段並管理覆蓋視圖的鍵盤。

enter image description here

+0

可以PLZ建議...如何設置一個文本框35 uivew –

+0

以UIScrollView並使用interfacebuilder添加所需的文本區域。使用界面構建器將加速您的任務。看到我更新的答案。 –

+0

rahul先生我不能添加textfiled下面的看法...... –

0

這可能是由於細胞的你出隊的財產,

您嘗試通過這樣的

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

需要在.h文件中

到uitextfieldDelegate在.m文件

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

}

+1

對不起priyanka,但它不工作......謝謝你的建議 –

+0

xib你需要連接所有的文本字段與代表... –

相關問題