【问题标题】:UITableView with UITextView scrolling issue带有 UITextView 滚动问题的 UITableView
【发布时间】:2013-03-18 18:48:30
【问题描述】:

我已经用一些自定义单元格组成了一个表格视图。一些自定义单元格包含UIWebView,一些包含UISlider 加上UITextField,一些包含UIButton。

现在,当我单击 UITableView 底部的 uitableviewcell 子视图的文本字段时,我无法让我的表格单元格在键盘上方正确可见。

我无法使用UITableViewController 代替UIViewController。因为我的 UI 结构有些奇怪和动态。简单来说就像

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

我也试过下面的代码让它工作。

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

但它不起作用:(

谢谢,

编辑 1:

我已经检查了 textfield、tableviewcell 和 tableview 的参考资料。所以对象的引用不是问题。

【问题讨论】:

  • 当键盘可见时,为什么不调整整个 UIView 的大小?
  • textFieldDidBeginEditing:() 方法是否触发?
  • @Lefteris:那么我必须调整其中的所有内容,我认为这不是个好主意。
  • @BhanuPrakash:是的,当然它正在开火。
  • @CrimeMasterGOGO 但是您使用的是 UITableView(将它嵌套在滚动视图中并不重要)。所以调整 UITableView 的大小实际上并不需要你做任何事情。它只会按照您的意愿向上移动单元格...

标签: ios xcode ipad uitableview


【解决方案1】:

试试这个代码....

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

这对你有帮助...

【讨论】:

    【解决方案2】:

    试试这个代码

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

    【讨论】:

      【解决方案3】:

      根据我的理解,当用户开始在UITableViewCell 中编辑UITextField 时,您希望显示UITextField 以及键盘。 如果是这种情况,您可以将UITableView 的contentInset 设置为所需的大小,然后只需滚动到所需的索引路径即可。

      以下是示例代码:

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

      【讨论】:

      • 你的tableview的位置在哪里。如果它位于屏幕底部或键盘覆盖其视图的某个位置,那么您还必须增加滚动视图的“contentSize”。将此添加到您的文本字段shouldBeginEditing CGPoint scrollPoint = CGPointMake(0,Required_Height); [your_scrollview setContentOffset:scrollPoint animated:YES];'
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2011-11-24
      • 2023-03-20
      • 2012-03-23
      相关资源
      最近更新 更多