简体   繁体   English

UITableView与UITextView滚动问题

[英]UITableView with UITextView scrolling issue

I have composed a tableview with some custom cells. 我已经用一些自定义单元格组成了一个tableview。 Some of the custom cells contain UIWebView , some has UISlider plus UITextField , some has UIButton . 一些自定义单元包含UIWebView ,一些具有UISlider加上UITextField ,一些具有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. 现在,当我单击UITableView底部的uitableviewcell子视图的文本字段时, 我无法设法使表格单元格在键盘上方正确可见。

Using of UITableViewController instead of UIViewController is not possible for me. 我无法使用UITableViewController而不是UIViewController Because my UI structure is somewhat weird and dynamic. 因为我的UI结构有些奇怪和动态。 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: 编辑1:

I have checked references for textfield, tableviewcell and tableview. 我已经检查了文本字段,tableviewcell和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 . 根据我的理解,当用户开始在UITableViewCell内部编辑UITextField时,您希望显示UITextField和键盘。 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. 如果是这种情况,可以将UITableViewcontentInset设置为所需的大小,然后滚动到所需的索引路径。

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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