我有一个自定义表视图的配方应用程序。问题是,当进入配方视图控制器并返回配方列表视图控制器时,它不保持先前的滚动位置。它总是回到顶部。
我想知道我的代码中是否包含某些内容,或者是否需要添加以保留以前的位置。
这是我的食谱列表视图控制器中的整个代码:
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
答案 0 :(得分:0)
向视图控制器添加变量以跟踪滚动视图的内容偏移量。在viewWillDisappear
方法中设置该变量,并在setContentOffset
方法中调用UITableView的viewWillAppear
方法:
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
}