經過大量的反覆試驗,我放棄並提出了問題。我見過很多有類似問題的人,但無法得到所有答案。帶UItextfield和動畫的UItableView?
我有一個UITableView,其中我有35個文本文件一個在另一個下面。
當我嘗試滾動和編輯UITableView底部的單元格時,我無法設法讓我的單元格正確定位在鍵盤上方。
我見過很多關於改變視圖大小的答案,但是目前爲止它們都沒有很好地工作。
任何人都可以澄清「正確」的方式來做到這一點與具體的代碼示例?
以下是我的.m文件:
#import "EditProfilePage.h"
@implementation EditProfilePage
#define kOFFSET_FOR_KEYBOARD 70.0
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat CELL_HEIGHT = 44;
static const CGFloat TOOLBAR_HEIGHT = 44;
static const CGFloat WINDOW_MINUS_TOOLBAR_HEIGHT_PORTRAIT = 460;
@synthesize scrollView,tableContents,txtField,allTextField,tableView;
@synthesize txtUserName ,txtFirstName ,txtLastName ,txtNickName,txtDisplayName,txtEmail,txtWebSite,txtAboutMe ,txtNewPassword ,txtPasswordAgain,btnSubmit ;
//extended profile information
@synthesize txtPayPalEmail ,txtActiveMemPk ,txtMemPkExpireDate,lbl,textFieldBeingEdited;
//business profile information
@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;
- (void)viewDidLoad
{
[super viewDidLoad];
allTextField=[[NSMutableArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
[self.scrollView setContentSize:CGSizeMake(320, (CELL_HEIGHT * [self.allTextField count]))];
![enter image description here][1][self.tableView setFrame:CGRectMake(0, 0,320, (CELL_HEIGHT * [self.allTextField count]))];
}
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
self.navigationController.navigationBarHidden=YES;
}
//
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
//self.navigationController.navigationBarHidden=NO;
}
-(void) keyboardWillShow:(NSNotification *)note
{
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
// keyboardHeight = keyboardBounds.size.height;
// if (keyboardIsShowing == NO)
{
//keyboardIsShowing = YES;
// CGRect frame = self.view.frame;
// frame.size.height -= keyboardHeight;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
//self.view.frame = frame;
[UIView commitAnimations];
}
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
CGFloat rowHeight = self.tableView.rowHeight;
if (textField == txtUserName.tag)
{
frame.origin.y += rowHeight * txtUserName.tag;
}
// else if (textField == textFields[CELL_FIELD_TWO])
// {
// frame.origin.y += rowHeight * CELL_FIELD_TWO;
// }
// else if (textField == textFields[CELL_FIELD_THREE])
// {
// frame.origin.y += rowHeight * CELL_FIELD_THREE;
// }
// else if (textField == textFields[CELL_FIELD_FOUR])
// {
// frame.origin.y += rowHeight * CELL_FIELD_FOUR;
// }
CGFloat viewHeight = self.tableView.frame.size.height;
CGFloat halfHeight = viewHeight/2;
CGFloat midpoint = frame.origin.y + (textField.frame.size.height/2);
if (midpoint < halfHeight)
{
frame.origin.y = 0;
frame.size.height = midpoint;
}
else
{
frame.origin.y = midpoint;
frame.size.height = midpoint;
}
[self.tableView scrollRectToVisible:frame animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// NSArray *lstData=[self.tableContents objectForKey:[self.txtField objectAtIndex:section]];
// return[lstData count];
return[allTextField count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier [email protected]"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
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]"User Name";
//txtUserName.backgroundColor = [UIColor grayColor];
[txtUserName addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
cell.accessoryView=txtUserName;
[email protected]"User Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
txtUserName.keyboardType = UIKeyboardTypeDefault;
txtUserName.returnKeyType = UIReturnKeyDone;
txtUserName.clearsOnBeginEditing = NO;
txtUserName.textAlignment = UITextAlignmentLeft;
// (The tag by indexPath.row is the critical part to identifying the appropriate
// row in textFieldDidBeginEditing and textFieldShouldEndEditing below:)
txtUserName.tag=indexPath.row;
//txtUserName.delegate = self;
txtUserName.clearButtonMode = UITextFieldViewModeWhileEditing;
[txtUserName setEnabled: YES];
[cell addSubview:txtUserName];
[txtUserName release];
//cell.font = [UIFont fontWithName:@"impact" size:10 line:2];
}
if ([indexPath row]==1)
{
txtFirstName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
[email protected]"First Name";
cell.accessoryView=txtFirstName;
[email protected]"First Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
if ([indexPath row]==2)
{
txtLastName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
[email protected]"Last Name";
cell.accessoryView=txtLastName;
[email protected]"Last Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
if ([indexPath row]==3)
{
txtNickName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
[email protected]"Nick Name";
cell.accessoryView=txtNickName;
[email protected]"Nick Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
if ([indexPath row]==4)
{
txtDisplayName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
[email protected]"Display Name";
cell.accessoryView=txtDisplayName;
[email protected]"Display Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
if ([indexPath row]==5)
{
txtEmail=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
[email protected]"Email";
cell.accessoryView=txtEmail;
[email protected]"Email ID:";
cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
return cell;
}
@end
你需要更具體的瞭解是什麼問題。嘗試用較小的代碼示例重現問題。 –
我有35個textfield tableview一個在另一個下面.....我想要移動textfield,當用戶觸摸texfield在鍵盤上.... plz幫助我出來.....謝謝你的先生 –
我想你我們需要向我們提供一個屏幕截圖,以便我們能夠充分了解問題。重申一下,縮短代碼,直到有儘可能少的代碼仍然會重現問題,然後使用新代碼和屏幕快照編輯您的問題。 –