gpt4 book ai didi

ios - 推送后如何将用户带到选定的索引

转载 作者:行者123 更新时间:2023-11-28 23:21:10 25 4
gpt4 key购买 nike

我如何才能将用户推送到他们选择的照片(选定的索引),并将他们推送到该照片,但仍然允许他们向上或向下滚动到该选定索引的上一张或下一张照片?

我遇到的问题是,当推送 viewController 时,它会将我带到第一个索引而不是选定的索引。

当点击一张照片时,我希望它推送到所选照片

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let singlePostVC = (storyboard!.instantiateViewController(withIdentifier: "SinglePostVC") as? SinglePostVC)!

singlePostVC.posts = posts
singlePostVC.selectedIndex = indexPath.row

navigationController?.pushViewController(singlePostVC, animated: true)
self.navigationController?.navigationBar.backIndicatorImage = #imageLiteral(resourceName: "BackButton")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: UIBarButtonItem.Style.plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem?.tintColor = UIColor(named: "darkModeLabels")

}

这是 SinglePostVC

的代码
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int.   {

return posts.count


}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SinglePostCell", for: indexPath) as! PostsCell

cell.delegate = self
cell.post = posts[indexPath.row]

handleUsernameLabelTapped(forCell: cell)
handleMentionTapped(forCell: cell)
handleHashTagTapped(forCell: cell)

return cell
}



func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}



override func viewDidLoad() {
super.viewDidLoad()


let indexPath = IndexPath(item: selectedIndex, section: 0)
postsCollectionView.scrollToItem(at: indexPath, at:.bottom , animated: true)

}

最佳答案

你应该把你的行:

let indexPath = IndexPath(item: selectedIndex, section: 0)
postsCollectionView.scrollToItem(at: indexPath, at:.bottom , animated: true)

viewDidAppear 方法中而不是像这样的 viewDidLoad :

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


let indexPath = IndexPath(item: selectedIndex, section: 0)
postsCollectionView.scrollToItem(at: indexPath, at:.bottom , animated: true)

}

发生的事情是你告诉它滚动到 CollectionView 的这个特定的 indexPath,但 CollectionView 并没有真正出现,所以一旦它出现它基本上会覆盖你告诉它执行的命令并像正常一样显示它.当您使用 viewDidAppear 时,它会一直等到 View 显示给用户,然后才显示滚动条。

关于ios - 推送后如何将用户带到选定的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com