【问题标题】:UITableView bigger than screen inside UIPageViewUITableView 比 UIPageView 内的屏幕大
【发布时间】:2014-08-25 10:44:07
【问题描述】:

最近我正因为这个而拔头发。我仍然是一名新手 iOS 开发人员,因此我将不胜感激。

我有一个具有三个视图的 UIPageViewController,中间视图是一个 UITableView,我以编程方式填充数据并尝试制作像网格一样的布局。当 UITableView 的数据过多时,信息会出现在屏幕之外。

我尝试禁用自动布局并将 UITableView 添加到 UIScrollView,使 UITableView 能够水平滚动。我已经设法做到了,但不知何故交互是混乱的,并且大多数时候在尝试水平滚动表格时它会捕获 UIPageViewController 交互。表格的垂直滚动也反应不佳。

对于这种情况有已知的解决方案吗?

【问题讨论】:

    标签: ios objective-c uitableview uiscrollview uipageviewcontroller


    【解决方案1】:

    在测试了一些东西后,我使用自动布局得到了一个可以接受的解决方案。

    所以首先我添加了一个 UIScrollView 并在 UIScrollView 中添加了 UITableView。 我在 UIScrollView 中启用了 Paging 并禁用了 Bounce Horizo​​ntally。

    在 UITableView 中,我基本上禁用了与弹跳分页和滚动相关的所有内容。

    由于数据以编程方式填充为某种网格,因此我必须知道最长行的总宽度,为此我创建了一些方法来计算最大宽度并排列标签。

    我还为表格 Width 和 Height 创建了约束,我在所有计算完成后计算并更新它们。

    -(void) ArrangeTable
    {
        for (int i= 0; i < [arrayTable count]; i++) {
            for (int j=0; j< [[arrayTable objectAtIndex:i ] count]; j++) {
                UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 8.0, 95.0, 30.0)];
                [nameLabel setText:[NSString stringWithFormat:@"%@",[[arrayTable objectAtIndex:i] objectAtIndex:j]]];
                [nameLabel sizeToFit];
    
                float widthIs = [nameLabel.text
                             boundingRectWithSize:nameLabel.frame.size
                             options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:@{ NSFontAttributeName:nameLabel.font }
                             context:nil].size.width;
                [self alignLabels:j currentWidth:[NSNumber numberWithFloat: widthIs]];
            }
        }
    }
    
    -(void) alignLabels:(int)colNumber currentWidth:(NSNumber*)labelWidth
    {
        if(colNumber >= [arrayLabelWidth count])
        {
            //Insert without position
            [arrayLabelWidth addObject:labelWidth];
    
        }else if ([labelWidth floatValue] > [[arrayLabelWidth objectAtIndex:colNumber] floatValue] )
        {
            [arrayLabelWidth replaceObjectAtIndex:colNumber withObject:labelWidth];
        }
    }
    
    -(void) setMaxRowWidth
    {
        float widthTV =[[arrayLabelWidth valueForKeyPath:@"@sum.self"] floatValue] + ([arrayLabelWidth count]) * 10;
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        float heightTV = [self tableViewHeight];
    
        if(widthTV > screenRect.size.width)
        {
            tvWidth.constant =  widthTV;
            //svWidth.constant = valueTV;
        }else{
            if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
                //DO Portrait
                tvWidth.constant =  screenRect.size.width;
            }else{
                //DO Landscape
                float calc = screenRect.size.width - self.view.frame.size.width;
                tvWidth.constant =  screenRect.size.width - calc;
            }
        }
        tvHeight.constant = heightTV;
    
    }
    
    - (CGFloat)tableViewHeight
    {
        [tablePod layoutIfNeeded];
        return [tablePod contentSize].height;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      相关资源
      最近更新 更多