在 UITableView 中垂直滚动时,在 ScrollView 标头上重置 contentOffset [英] contentOffset is reset on ScrollView header when vertical scrolling in UITableView

查看:21
本文介绍了在 UITableView 中垂直滚动时,在 ScrollView 标头上重置 contentOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我利用这段代码在 UITableView 中水平对齐了大量的 TruckDayScrollViews(UIScrollView 的子类),SUDaysForTruckView 只是一个自定义的 UILabel

Im making use of this code to align a load of truckDayScrollViews (Subclass of UIScrollView) horizontally in a UITableView, SUDaysForTruckView is just a custom UILabel

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    if ([scrollView isKindOfClass:[SUDaysForTruckView class]]) {
        CGPoint offset=CGPointMake(scrollView.contentOffset.x, 0);
        for (TruckViewCell *cell in self.tableView.visibleCells) {
            cell.truckDayScrollView.contentOffset=offset;

        }
        self.headerView.contentOffset=offset;
        self.scrollViewPosition=offset;
    }
    else{
        NSLog(@"Table view scrolling vertically: %f,%f",self.scrollViewPosition.x,self.scrollViewPosition.y);
        self.headerView.contentOffset=self.scrollViewPosition;
    }
}

想法是,如果滚动滚动视图的类是单元格的一部分(水平滚动),则更新所有滚动视图,包括标题.这很好用.

The idea being, that if the class of the scrolling scroll view is part of the cell (scrolling horizontally) then update all the scrollviews, including the header. This works great.

虽然tableView垂直滚动时,header滚动视图中的内容偏移被重置为0,0.

Although when the tableView scrolls vertically, the content offset in the header scroll view is reset to 0,0.

这是为什么,有没有办法解决.我试过把这条线

Why is this, and is there a way to fix it. I have tried putting the line

    self.headerView.contentOffset=self.scrollViewPosition;

在很多地方cellForRow,self.headerView等的getter.一切都无济于事.

in so many places cellForRow, the getter for self.headerView,etc. all to no avail.

第一张图显示了水平滚动和标题的对齐方式,第二张图显示了垂直滚动的第一个位发生了什么,标题立即跳出对齐.

The first picture shows horizontal scrolling and how the header is aligned, the second picture shows what happens on the very first bit of vertical scrolling, the headers jump out of alignment immediately.

推荐答案

我最终覆盖了 drawRect:我在垂直滚动之前获得了 scrollViews 的当前内容偏移量.然后我需要一点额外的东西来阻止我的所有视图都有黑色背景.

I ended up overriding drawRect: I get the current content offset of the scrollViews before vertical scrolling. And then I needed a little bit extra to stop all my views having a black background.

- (void)drawRect:(CGRect)rect
{
     [super drawRect:rect];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat white[] = {1, 1, 1, 1};
    CGContextSetFillColor(ctx, white);
    CGContextAddRect(ctx, rect);
    CGContextFillPath(ctx);
    self.contentOffset=[self.scrollViewMaster scrollViewPosition];
}

这篇关于在 UITableView 中垂直滚动时,在 ScrollView 标头上重置 contentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆