【发布时间】:2012-01-06 03:16:03
【问题描述】:
经过大量的反复试验,我放弃并提出这个问题。我见过很多人有类似的问题,但无法得到所有正确的答案。
我有一个 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 =@"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)];
txtUserName.placeholder=@"User Name";
//txtUserName.backgroundColor = [UIColor grayColor];
[txtUserName addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
cell.accessoryView=txtUserName;
cell.textLabel.text=@"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)];
txtFirstName.placeholder=@"First Name";
cell.accessoryView=txtFirstName;
cell.textLabel.text=@"First Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
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:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
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:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
if ([indexPath row]==4)
{
txtDisplayName=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
txtDisplayName.placeholder=@"Display Name";
cell.accessoryView=txtDisplayName;
cell.textLabel.text=@"Display Name:";
//cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
if ([indexPath row]==5)
{
txtEmail=[[UITextField alloc] initWithFrame:CGRectMake(0,0,150,25)];
txtEmail.placeholder=@"Email";
cell.accessoryView=txtEmail;
cell.textLabel.text=@"Email ID:";
cell.font = [UIFont fontWithName:@"Helvetica" size:14];
}
return cell;
}
@end
【问题讨论】:
-
您需要更具体地了解问题所在。尝试用更小的代码示例重现问题。
-
我在 tableview 上有 35 个文本字段,一个在另一个下方.....当用户在键盘上触摸 texfield 时,我想将文本字段向上移动....请帮帮我.... .谢谢先生
-
我认为您需要向我们提供屏幕截图,以便我们能够完全理解问题所在。重申一下,请缩短代码,直到您有尽可能少的代码仍能重现问题,然后使用新代码和屏幕截图编辑您的问题。
-
我试图上传屏幕短... jst检查那个页面...谢谢先生