after a lot of trial and error, I'm giving up and asking the question. I've seen a lot of people with similar problems but can't get all the answers to work right.
I have a UITableView in which i have 35 textfiled one below the other.
When I try to scroll and edit the cells at the bottom of the UITableView, I can't manage to get my cells properly positioned above the keyboard.
I have seen many answers talking about changing view sizes,etc... but none of them has worked nicely so far.
Could anybody clarify the "right" way to do this with a concrete code example?
following is my .m file:
#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