【问题标题】:How to scroll to a particular index in collection view in swift 4?如何在swift 4中滚动到集合视图中的特定索引?
【发布时间】:2018-12-23 15:19:36
【问题描述】:

我已经搜索了与此问题相关的各种链接,但无法解决此问题,这就是我发布此问题的原因。我有一个collectionView。在一个特定的单元格中,我有许多视图,例如 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) })
}

但我的问题是:

  1. 我必须在哪里编写此代码?在视图中确实出现了?或在 评论发帖功能哪里成功了?
  2. 如何将 indexNumber 和 sectionNumber 传递给

    self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section:sectionNumber)

请任何人帮助我解决这个问题。谢谢

【问题讨论】:

  • 你可以使用其他类来添加评论吗??
  • 我正在使用添加评论的功能。调用 api 的地方。如有必要,我可以使用其他课程。
  • 你在哪里调用你的帖子评论方法?从一个按钮的动作可能是什么?
  • 我使用了自定义委托
  • 你能显示那个代码吗?

标签: ios uicollectionview swift4 uicollectionviewcell uicollectionviewdelegate


【解决方案1】:

您可以使用其中一种生命周期方法,甚至可以在重新加载数据调用之后使用。像这样的,

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

谢谢。

【讨论】:

  • 让 indexPath = IndexPath(item: 12, section: 0) 先生 item 12 是什么意思?
  • 第 12 项是您要在第 0 部分滚动的手机号码
  • 就像我们使用 indexPath.row 方法的 UITableView 一样,在 collectionView 中我们更喜欢使用 IndexPath.Item 因为 collectionView 可以是 Vertical 或 Horizo​​ntal。
  • 这意味着我必须在 IndexPath(item: 12, section: 0) 中传递 IndexPath.Item 。对吧,先生?
  • 不不,你正在创建 IndexPath,你所要做的就是找到你评论的正确位置,假设你有一个 cmets 数组,它的大小现在是 12 用户发布的新评论,现在数组的大小是多少???? 12+1 = 13 对 ???并且因为数组有 0 索引所以位置将是 12 ???好的。简单传球 (cmetsArray.length -1) 作为你的位置
【解决方案2】:

根据您展示的自定义委托方法:

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。希望这可以帮助。 :)

【讨论】:

  • 是的,我得到了这样的结果 let indexPath = collectionView.indexPath(for: cell)
  • 这意味着在滚动到项目时我必须通过这个 indexPath?对吧,先生?
  • 正是兄弟。 bolar kono dorkar nei bhai 爵士!
  • 非常感谢 \m/
  • 我们是孟加拉人。 :) 但是 mu odiya bhi janichi tike tike。 :)
猜你喜欢
  • 2018-12-23
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2019-02-09
  • 2019-04-02
  • 1970-01-01
相关资源
最近更新 更多