简体   繁体   中英

UITableView with UITextView scrolling issue

I have composed a tableview with some custom cells. Some of the custom cells contain UIWebView , some has UISlider plus UITextField , some has UIButton .

Now when I click on textfield which is uitableviewcell's subview at the bottom of the UITableView , I can't manage to get my table cell properly visible above the keyboard.

Using of UITableViewController instead of UIViewController is not possible for me. Because my UI structure is somewhat weird and dynamic. In simple way it is like

UIView -> UIScrollView with paging -> x number of UITableViews -> y number of UITableViewCell ->  z number of UITextFields, UISliders, UIWebViews, UIButtons, UILables, etc.

I have also tried below code to make it work.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
    [table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

but it is not working :(

Thanks,

EDIT 1:

I have checked references for textfield, tableviewcell and tableview. So reference of object is not the problem.

Try this code....

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
        UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
        NSIndexPath *indexPath = [[table_Question[pageNumber] indexPathForCell:cell];

        int totalRows = 0;
        if([[table_Question[pageNumber] numberOfSections] > 1)
         {
              for(int i = 0; i<([indexPath.section] - 1);i++)
               {
                     totalRows += [[table_Question[pageNumber] numberOfRowsInSection:i];
                }
          }

          totalRows += indexPath.row;

        int  yPosition = totalRows * cell.frame.size.height;
        CGPoint offset = CGPointMake(0, yPosition);
        [[table_Question[pageNumber] setContentOffset:offset animated:YES];
}

This will helps for you...

As per my understanding, you want to show the UITextField as well as the keyboard, when the user starts editing the UITextField inside the UITableViewCell . If that's the case, you can set the contentInset of the UITableView to the required size and then just scroll to the required index path.

Following is a sample code:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    @try
    {
        table_Question[pageNumber].contentInset = UIEdgeInsetsMake(CGFloat Req_top, CGFloat Req_left, CGFloat Req_bottom, CGFloat Req_right);
        [table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];

        //Add this to you code to scroll in the tableview gets hidden by keyboard
        CGPoint scrollPoint = CGPointMake(0, Required_Height);
       [yourScrollView setContentOffset:scrollPoint animated:YES];
    }
}

Try this code

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    UITableViewCell *aCell = [table_Question[pageNumber] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    CGSize cellSize = aCell.frame.size;
    [table_Question[pageNumber] setContentOffset:CGPointMake(0, cellSize.height*2) animated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM