简体   繁体   English

UICollectionView在滚动嵌套的UITableView后重置内容偏移量

[英]UICollectionView resets content offset after scrolling a nested UITableView

Setup: 设定:

I have a single UICollectionView created on a Storyboard and delegates connected to a controller, oriented for horizontal scroll. 我在Storyboard上创建了一个UICollectionView,并且连接到控制器的委托,面向水平滚动。 Each UICollectionViewCell has a UITableView inside, delegated to the same controller. 每个UICollectionViewCell都有一个UITableView,委托给同一个控制器。 The Collection view is paged. Collection视图已分页。 There are about 3.5 cells visible at a time. 一次可见约3.5个细胞。

Problem: 问题:

If I scroll over horizontally on the collection view so that is now "paged" over 1 length, and then attempt to scroll a contained UITableView vertically, the UICollectionView UNINTENTIONALLY content offset resets by animating to content offset of 0,0. 如果我在集合视图上水平滚动以便现在“分页”超过1个长度,然后尝试垂直滚动包含的UITableView,则UICollectionView UNINTENTIONALLY内容偏移将通过设置动画到内容偏移量0,0来重置。 The touch event is maintained, and the dequeued cell continues its scrolling action. 保持触摸事件,并且出队的单元继续其滚动动作。 I do not want the collection view to reset when a user scrolls the tableview 当用户滚动tableview时,我不希望重置集合视图

note: 注意:

using the iPad simulators 使用iPad模拟器

样本图片 Here is a sample picture. 这是一张示例图片。 Each column is a UICollectionViewCell containing a UITableView. 每列都是包含UITableView的UICollectionViewCell。 Each row within the column is a UITableViewCell. 列中的每一行都是UITableViewCell。 If I am not at Content off set of 0,0 for the UICollectionView, and I scroll on any TableView, the collection view animates back to having a content offset of 0,0 如果我不是UICollectionView的0的内容集,并且我在任何TableView上滚动,则集合视图会动画回到内容偏移量为0,0

Little bit outdated answer, but might help someone. 有点过时的答案,但可能会帮助别人。 First set scrollView delegate in collectionView to return to main view controller. 首先在collectionView中设置scrollView委托以返回主视图控制器。

Declare in view controller: 在视图控制器中声明:

private var scrollViewOffset: CGPoint = CGPointZero

In main view controller add the following code: 在主视图控制器中添加以下代码:

func scrollViewDidScroll(scrollView: UIScrollView) {

        if scrollView == self.tableView {
            return
        }

        self.scrollViewOffset = scrollView.contentOffset

        for cell in self.tableView.visibleCells {
            (cell as! MyCell).collectionView.contentOffset = scrollView.contentOffset
        }
    }

After you did this part, next one is what you're missing: 完成这一部分之后,下一部分就是您所缺少的部分:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: MyCell = tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as! MyCell           
        cell.delegate = self

        cell.collectionView.reloadData()
        cell.collectionView.layoutIfNeeded() // THIS PART IS NEEDED IN ORDER NOT TO SCROLL TO 0, 0 OFFSET
        return cell
    }

And in: 并在:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let cell: MyCell = cell as! MyCell
        cell.collectionView.contentOffset = self.scrollViewOffset
    }

Content of the MyCell should be something like this: MyCell的内容应该是这样的:

protocol MyCellDelegate {
        func scrollViewDidScroll(scrollView: UIScrollView)
    }
    class MyCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    var delegate: MyCellDelegate? = nil

// Do collection view display logic here
// ...
// After that implement scrollview delegate

    func scrollViewDidScroll(scrollView: UIScrollView) {
            if (self.delegate != nil) {
                self.delegate?.scrollViewDidScroll(scrollView)
            } else {
                print("Missing delegate init")
            }
        }
}

This answer is for Xcode 7.3 这个答案适用于Xcode 7.3

Nested scroll views have undefined scroll behavior in iOS last time I checked. 我上次检查时,嵌套滚动视图在iOS中有未定义的滚动行为。 You may be able to override behavior to suit your purposes by extending (or subclassing) the table or collection views and ignoring certain events by checking the sender (or sender's identifier) in content mutator methods. 您可以通过扩展(或子类化)表或集合视图来覆盖行为以适合您的目的,并通过检查内容更改器方法中的发送方(或发送方标识符)来忽略某些事件。

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

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