Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift 4中滚动到集合视图中的特定索引?_Ios_Uicollectionview_Swift4_Uicollectionviewcell_Uicollectionviewdelegate - Fatal编程技术网

Ios 如何在swift 4中滚动到集合视图中的特定索引?

Ios 如何在swift 4中滚动到集合视图中的特定索引?,ios,uicollectionview,swift4,uicollectionviewcell,uicollectionviewdelegate,Ios,Uicollectionview,Swift4,Uicollectionviewcell,Uicollectionviewdelegate,我已经搜索了与此问题相关的各种链接,但无法解决此问题,这就是我发布此问题的原因。我有一个收藏视图。在一个特定的单元格中,我有许多视图,如UserProfileView、ProductView、CommentBoxView等。在CommentBoxView中有一个textView,用户可以在其中键入并发布评论。发布评论后,我想滚动该特定单元格,该单元格用户已从中发布评论。我知道我必须使用self.collectionView.scrollToItem(at:IndexPath(item:index

我已经搜索了与此问题相关的各种链接,但无法解决此问题,这就是我发布此问题的原因。我有一个
收藏视图
。在一个特定的单元格中,我有许多视图,如UserProfileViewProductViewCommentBoxView等。在CommentBoxView中有一个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(位于:IndexPath(项目:indexNumber,部分:sectionNumber)


  • 请任何人帮我解决这个问题。谢谢你可以使用生命周期方法之一,甚至在你重新加载数据调用之后。类似这样的

    override func viewDidLayoutSubviews() 
    {
        super.viewDidLayoutSubviews()
        let indexPath = IndexPath(item: 12, section: 0)
        self.collectionView.scrollToItem(at: indexPath, at: [.centeredVertically,   .centeredHorizontally], animated: true)
    }
    

    谢谢。

    按照您显示的自定义委托方法:

    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) 
        })
    
    我相信您正在将
    单元格
    从您的单元格类传递到控制器。因此,在自定义委托方法中,您可以通过如下方式获得indexPath:

    let indexPath = yourCollectionView.indexPath(for: yourCollectionCell)
    

    现在您可以滚动到此indexPath。希望这有帮助。:

    您可以使用另一个类来添加注释吗?我正在使用一个函数来添加注释。调用api的位置。如果有必要,我可以使用另一个类。你在哪里调用你的post-comment方法?从按钮的动作可能是?我使用了自定义delegates,你能显示代码吗?让indexPath=indexPath(项:12,节:0)sir项12的意思是?项12是你想要在节0中滚动的单元格号,就像我们使用indexPath.row方法的UITableView一样,在collectionView中,我们更喜欢使用IndexPath.Item,因为collectionView可以是垂直的,也可以是水平的。这意味着我必须在IndexPath中传递IndexPath.Item(项:12,节:0)。对吗,先生?不,不,你正在创建IndexPath,你所要做的就是找到你评论的正确位置,假设你有一个评论数组,它的大小是12,现在用户发布了一条新评论,现在数组的大小是多少????12+1=13对吗???因为数组的索引为0,所以位置将为12???可以简单传递(commentsArray.length-1)作为您的位置是的,我是这样得到的让indexath=collectionView.indexath(for:cell)这意味着在滚动到项中我必须传递这个indexath?对吧,先生?没错,兄弟。博拉尔·科诺·多卡尔·内拜爵士!非常感谢(我/我们是孟加拉人。)但是木奥迪亚·比詹尼奇·蒂克·蒂克。:)