簡體   English   中英

帶有UItextfield和動畫的UItableView?

[英]UItableView with UItextfield and animation?

經過大量的反復試驗后,我放棄了,問了一個問題。 我見過很多有類似問題的人,但無法獲得所有正確答案的答案。

我有一個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

您需要使滾動視圖更高,以便可以滾動鍵盤上方的下部文本字段。

這是一篇說明如何執行此操作的文章:

http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM