UITableViewCell 中的滚动视图不会保存位置 [英] Scroll View in UITableViewCell won't save position

查看:23
本文介绍了UITableViewCell 中的滚动视图不会保存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UITableView 中有一些 UIScrollView.

我的问题是,如果我将第一个单元格中的滚动视图滚动到另一个位置,第四个单元格也将位于同一位置.

My problem is that if I scroll the scroll view in the first cell to another position, the fourth cell will also be at that same position.

第二个和第五个,第三个和第六个一样......

Same for the second and the fifth, the third and the sixth...

有没有办法让单元格中的滚动视图保持它们的位置?

Is there a way for the scroll views in the cells to keep their position?

推荐答案

您应该将 UIScrollView 的实际内容偏移量保存在一个数组中,在自定义委托中发生滚动后检索该值并将偏移量设置为 cellForRowAt 中保存的值.

You should save the actual content offsets of your UIScrollViews in an array, retreive the value after a scroll happened in a custom delegate and set the offset to the saved value in cellForRowAt.

CustomCell.swift

weak var delegate: CellScrollViewDelegate?
let scrollView: UIScrollView!
var contentOffset: CGPoint!

func setUpCell() {
    scrollView.delegate = self
    scrollView.contentOffset = savedContentOffset
}


[...]

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    delegate?.horizontalCellDidScroll(indexPath: indexPath, contentOffset: scrollView.contentOffset)
}

TableViewController.swift

var cellScrollContentOffsets = [[CGPoint]]()

func horizontalCellDidScroll(indexPath: IndexPath, contentOffset: CGPoint) {
    cellScrollContentOffsets[indexPath.section][indexPath.row] = contentOffset
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
    cell.delegate = self
    cell.savedContentOffset = cellScrollContentOffsets[indexPath.section][indexPath.row]
    cell.setUpCell()

    return cell
}

Delegate.swift

protocol CellScrollViewDelegate: class {
    func horizontalCellDidScroll(indexPath: IndexPath, contentOffset: CGPoint)
}

这篇关于UITableViewCell 中的滚动视图不会保存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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