我搜索了与此问题相关的各种链接,但无法解决此问题,这就是我发布此问题的原因。我有一个collectionView
。在特定的单元格中,我有许多视图,例如 UserProfileView , ProductView , CommentBoxView 等。在 CommentBoxView 中,有一个< strong> textView ,用户可以在其中键入然后发表评论。发布评论后,我要滚动该特定单元格,该单元格用户已从该单元格中发布了评论。我知道我必须使用self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false)
这是代码
func customCell(_ cell: UICollectionViewCell, didPressButton: UIButton, indexPathRow: Int, commentPost: String, commentatorImage: UIImageView, commentatorName: UILabel, comments: UITextView) {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
self.collectionView.reloadData()
self.postComment(commentatorImage: commentatorImage, commentatorName: commentatorName, comments: comments) })
}
但是我的问题是:
如何将indexNumber和sectionNumber传递给
self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section:sectionNumber)
关于此事,请任何人帮助我。谢谢
答案 0 :(得分:1)
您可以使用生命周期方法之一,甚至可以在重载数据调用之后使用。像这样的东西
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let indexPath = IndexPath(item: 12, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: [.centeredVertically, .centeredHorizontally], animated: true)
}
谢谢。
答案 1 :(得分:0)
根据您显示的自定义委托方法:
func customCell(_ cell: UICollectionViewCell, didPressButton: UIButton, indexPathRow: Int, commentPost: String, commentatorImage: UIImageView, commentatorName: UILabel, comments: UITextView) {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
self.collectionView.reloadData()
self.postComment(commentatorImage: commentatorImage, commentatorName: commentatorName, comments: comments)
})
我相信您正在将cell
从单元格类传递到控制器。因此,在您的自定义委托方法中,您可以按照以下方式获取indexPath:
let indexPath = yourCollectionView.indexPath(for: yourCollectionCell)
现在,您可以滚动到此indexPath。希望这可以帮助。 :)