保留嵌套collectionView的滚动位置 [英] retain scroll position for nested collectionView

查看:90
本文介绍了保留嵌套collectionView的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tableView.reloadData()之后,可见的 collectionView 立即显示意外的第一行。

After the tableView.reloadData() the visible collectionView display the first row unexpected immediately.

我正在构建一个 tableView 在其单元格中包含 collectionView ,用户可以滚动就像Instagram,在每个 tableView 中都有多个图像。我该如何解决?谢谢!

Im building a tableView contains collectionView in its cells, users can scroll multiple images in every single tableView just like Instagram. How can I fix it? Thanks!

tableView数据源

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return photoRolls.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! HomeTableViewCell
    if photoRolls.isEmpty {
        return UITableViewCell()
    }        
    let user: UserModel = users[indexPath.row]
    let photoRoll: PhotoRoll = photoRolls[indexPath.row] //this Model contains post info: likes, comments etc.
    let photoUrls: UrlStrings = urls[indexPath.row] //this Model contains a array of urlStrings for each collectionView inside the tableViewCell
    cell.urlStrings = photoUrls
    cell.photoRoll = photoRoll
    cell.user = user

    cell.delegate = self

    return cell
}

tableViewCell中的prepareForReuse方法

override func prepareForReuse() {
    super.prepareForReuse()
    captionLabel.text = nil
    profileImage.image = UIImage(named: "placeholderImg")
    profileImageRight.image = UIImage(named: "placeholderImg")
    collectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .left, animated: false)//tried to remove this method, but the collectionView  would not display the first row when it's visible
}

tableViewCell内collectionView的数据源

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return cellUrlArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell
    cell.url = cellUrlArray[indexPath.row]
    return cell
}

就像问题标题说的那样。我希望 tableView tableView之后加载更多数据后,可见的 collectionView 保留在当前行上.reloadData()被调用!再次感谢!

Like the question title said. I expect the visible collectionView stays on the current row after the tableView load more data after tableView.reloadData() is called! Thanks again!

推荐答案

我认为使用 contentOffset 缓存是可行的,如下所示

I think it is possible with contentOffset cacheing, like below

var cachedPosition = Dictionary<IndexPath,CGPoint>()

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if let cell = cell as? HomeTableViewCell {
        cachedPosition[indexPath] = cell.collectionView.contentOffset
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! HomeTableViewCell
    let user: UserModel = users[indexPath.row]
    let photoRoll: PhotoRoll = photoRolls[indexPath.row] //this Model contains post info: likes, comments etc.
    let photoUrls: UrlStrings = urls[indexPath.row] //this Model contains a array of urlStrings for each collectionView inside the tableViewCell
    cell.urlStrings = photoUrls
    cell.photoRoll = photoRoll
    cell.user = user

    cell.collectionView.contentOffset = cachedPosition[indexPath] ?? .zero

    cell.delegate = self

    return cell
}

这篇关于保留嵌套collectionView的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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