保持UITableView和UICollectionView滚动位置同步

时间:2015-05-19 07:51:24

标签: ios objective-c iphone uitableview uicollectionview

我有UIViewController能够通过UICollectionView(cv)或UITableView(电视)显示其内容。

两个视图都通过故事板添加到视图层次结构中,我只是隐藏了一个不应该显示的视图。 UICollectionViewUITableView都具有相同的数据源。 UICollectionView显示每行两个项目中的内容,UITableView显然是每行一个项目。

所以我想要发生的是,如果用户决定在视图之间切换,新显示的视图应该滚动到另一个视图中可见的元素。

我做了一些scrollToRowAtIndexPath:atScrollPosition:animated:scrollToItemAtIndexPath:atScrollPosition:animated:的试验但没有成功。

我理解,UICollectionViewUITableView都能够返回其当前可见项'/ rows'indexPaths,但是我如何将这些indexPath转换为另一个视图的indexPath并告诉他们滚动到那个元素?

我当前对开关按钮的触摸事件处理如下所示:

- (IBAction)switchView:(id)sender {
    
    self.showListView = !self.showListView;
    
    UIView *fromView, *toView;
    NSIndexPath *indexPath;
    
    if (self.showListView)
    {
        fromView = self.ArticleCollection;
        toView = self.ArticleList;
        
        CGRect visibleRect = (CGRect){.origin = self.ArticleCollection.contentOffset, .size = self.ArticleCollection.bounds.size};
        CGPoint visiblePoint = CGPointMake(100, CGRectGetMidY(visibleRect));
        indexPath = [self.ArticleCollection indexPathForItemAtPoint:visiblePoint];
    }
    else
    {
        fromView = self.ArticleList;
        toView = self.ArticleCollection;
        
        CGRect visibleRect = (CGRect){.origin = self.ArticleList.contentOffset, .size = self.ArticleList.bounds.size};
        CGPoint visiblePoint = CGPointMake(100, CGRectGetMidY(visibleRect));
        indexPath = [self.ArticleList indexPathForRowAtPoint:visiblePoint];
    }
    
    NSInteger row = indexPath.row;
    
    if (self.showListView) {
        [self.ArticleList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row*2 inSection:0]
                                atScrollPosition:UITableViewScrollPositionTop
                                        animated:NO];
    } else {
        [self.ArticleCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:ceil(row/2)
                                                                            inSection:0
                                                                     atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                                                             animated:NO]];
    }
    
    // Here a transitionFromView:ToView:duration:options:completion call follows
}

提前感谢您对我的问题的任何贡献。

1 个答案:

答案 0 :(得分:0)

来自 scrollToItemAtIndexPath:atScrollPosition:animated:电话中的一个小错误:

[self.ArticleCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:row
                                                                        inSection:0]
                                                                 atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                                                         animated:NO];

我确实认为 indexPath.row 会包含不同的值,无论它来自cv的可见indexPath还是tv的可见indexPath,所以我通过乘以或计算新行除了2.但事实证明在两种情况下行都是相同的,所以我只需要在两种情况下都使用相同的值:

NSInteger row = indexPath.row;

if (self.showListView) {
    [self.ArticleList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]
                            atScrollPosition:UITableViewScrollPositionTop
                                    animated:NO];
} else {
    [self.ArticleCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:row
                                                                        inSection:0]
                                                                 atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                                                         animated:NO];
}

很抱歉给您带来不便。

相关问题