【问题标题】:Maintain Scroll Position When Changing View Controller更改视图控制器时保持滚动位置
【发布时间】:2017-09-17 21:08:17
【问题描述】:

我有一个带有自定义表格视图的食谱应用。问题是,当进入配方视图控制器并返回配方列表视图控制器时,它不会保持先前的滚动位置。它总是回到顶部。

我想知道这是否是我的代码中的某些内容,或者是否是我需要添加的内容以保持以前的位置。

这是我的食谱列表视图控制器的完整代码:

var recipeImgs: [UIImage] = [UIImage(named: "bananaPancakesCat.jpg")!,UIImage(named: "blackEyedPeaPattiesCat.jpg")!,UIImage(named: "blueberryMuffinsCat.jpg")!, UIImage(named: "buckwheatWafflesCat.jpg")!, UIImage(named: "cashewCreamCheeseCat.jpg")!, UIImage(named: "frenchToastCat.jpg")!, UIImage(named: "fruitSyrupCat.jpg")!, UIImage(named: "hashBrownsCat.jpg")!, UIImage(named: "proteinBarsCat.jpg")!, UIImage(named: "smoothieCat.jpg")!, UIImage(named: "tempehBaconCat.jpg")!, UIImage(named: "tofuQuicheCat.jpg")!]

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

override func viewDidDisappear(_ animated: Bool) {
    self.tableView.removeFromSuperview()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "RecipesTableViewCell") as? RecipesTableViewCell {
        cell.configureCell(recipeImgs[(indexPath as NSIndexPath).row])
        return cell

    } else {
        return RecipesTableViewCell()
    }

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (UIDevice.current.userInterfaceIdiom == .pad) {
        return 120
    }
        return 90.0
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if (indexPath as NSIndexPath).row == 0 {
        performSegue(withIdentifier: "BRVC1", sender: self)
    } else if (indexPath as NSIndexPath).row == 1 {
        performSegue(withIdentifier: "BRVC2", sender: self)
    } else if (indexPath as NSIndexPath).row == 2 {
        performSegue(withIdentifier: "BRVC3", sender: self)
    } else if (indexPath as NSIndexPath).row == 3 {
        performSegue(withIdentifier: "BRVC4", sender: self)
    } else if (indexPath as NSIndexPath).row == 4 {
        performSegue(withIdentifier: "BRVC5", sender: self)
    } else if (indexPath as NSIndexPath).row == 5 {
        performSegue(withIdentifier: "BRVC6", sender: self)
    } else if (indexPath as NSIndexPath).row == 6 {
        performSegue(withIdentifier: "BRVC7", sender: self)
    } else if (indexPath as NSIndexPath).row == 7 {
        performSegue(withIdentifier: "BRVC8", sender: self)
    } else if (indexPath as NSIndexPath).row == 8 {
        performSegue(withIdentifier: "BRVC9", sender: self)
    } else if (indexPath as NSIndexPath).row == 9 {
        performSegue(withIdentifier: "BRVC10", sender: self)
    } else if (indexPath as NSIndexPath).row == 10 {
        performSegue(withIdentifier: "BRVC11", sender: self)
    } else {
        performSegue(withIdentifier: "BRVC12", sender: self)
    }
}

还有一个视频来演示正在发生的事情:https://www.dropbox.com/s/g6jy37t97d1ihft/ScreenRecording_09-19-2017%2015%3A31.mp4?dl=0

【问题讨论】:

  • 您可以在 viewController 中保留一个 var,保持最后一个 offset.y 位置,并在加载后使用 self.tableView.setContentOffset(<#T##contentOffset: CGPoint##CGPoint#>, animated: <#T##Bool#>) 进行滚动
  • 你如何回到原来的视图控制器?您确定不是显示新的列表视图控制器,而是返回到现有的列表视图控制器?
  • @rmaddy 故事板转场。这是我的问题吗?
  • 我不知道,我不使用情节提要。只需确保您的 segue 只是返回到以前的控制器而不是新的控制器。

标签: ios swift uitableview tableview


【解决方案1】:

向视图控制器添加一个变量来跟踪滚动视图的内容偏移量。在viewWillDisappear方法中设置该变量,并在viewWillAppear方法中调用UITableView的setContentOffset方法:

private var contentOffset: CGPoint?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let contentOffset = self.contentOffset {
        self.tableView.setContentOffset(contentOffset, animated: true)
    }
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    self.contentOffset = self.tableView.contentOffset
}

【讨论】:

  • 但这些都不是必需的。简单地转到另一个视图控制器并返回不会更改表格视图的滚动偏移量。除了简单地重置滚动偏移量之外,还有更多问题。
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多