【问题标题】:How to set focus on collectionView in swift 4?如何在 swift 4 中设置对 collectionView 的关注?
【发布时间】:2018-12-22 13:05:19
【问题描述】:

您好,我有一个UICollectionView。在一个特定的单元格中,我有很多视图,例如UserProfileViewProductViewCommentBoxView 等。在CommentBoxView 中有一个文本视图,用户可以在其中键入然后发表评论。发表评论后,我想关注那个特定的单元格,用户从哪个单元格发布了评论。为此我使用过:

func indexPathForPreferredFocusedView(in collectionView: UICollectionView) -> IndexPath? {
  return IndexPath(row: 2, section: 0)
}

我正在使用硬编码值进行测试,但它仍然没有到来。请任何人帮助我解决这个问题。

【问题讨论】:

    标签: ios uicollectionview swift4 uicollectionviewcell uicollectionviewdelegate


    【解决方案1】:

    尝试使用:

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

    【讨论】:

    • 这段代码在哪里写?发表评论后?
    • 先生如何在collectionView.scrollToItem中传递indexPath(at:indexPath,scrollPosition:.centeredVertically,动画:true)?
    • 先生,我会的
    • 实际上先生,我有一个collectionView。在一个特定的单元格中,我有许多视图,例如 UserProfileView、ProductView、CommentBoxView 等。在 CommentBoxView 中有一个 textView,用户可以在其中键入然后发表评论。发表评论后,我想滚动那个特定的单元格,用户从哪个单元格发布了评论。我知道我必须使用 self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false)
    • 但它没有运行
    【解决方案2】:

    也许“焦点”是指“滚动到”。如果你想滚动到一个单元格,你应该调用

    collectionView.scrollToItem(at: indexPath, scrollPosition: .centeredVertically, animated: true)

    查看documentation 以了解此功能可能会很有用。

    据我所知,focus 是 tvOS 开发的核​​心概念,但在 iOS 中,它的主要功能是推动无障碍体验。因此,我认为它不会达到您所期望的效果。

    【讨论】:

    • 这段代码在哪里写?发表评论后?
    • 先生如何在collectionView.scrollToItem中传递indexPath(at:indexPath,scrollPosition:.centeredVertically,动画:true)?
    • 试试collectionView.scrollToItem(at: IndexPath(item: 2, section: 0), scrollPosition: .centeredVertically, animated: true)
    • @GoribDeveloper 你需要在 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 方法中编写这段代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多