ctionView单元格中的textView成为firstRes

ctionView单元格中的textView成为firstRes

本文介绍了导航单元格时,使collectionView单元格中的textView成为firstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个collectionView,并允许用户动态地将单元格添加到collectionView。视图控制器一次只显示一个单元格,并且我希望第一个textView(它是单元格的一部分)成为firstResponder(并始终保持键盘可见),这在加载视图控制器时也可以正常工作

I have a collectionView and allow a user to dynamically add cells to the collectionView. The view controller only shows one cell at a time and I want the first textView (which is part of the cell) to become the firstResponder (and keep the keyboard visible at all times), which works fine when loading the view controller (as well as in one of the cases below).

我创建了一种检测当前单元格的方法,在以下任何一种情况下,我都会每次调用该方法:(1)用户从一个单元格滚动到另一个单元格(方法放置在scrollViewWillEndDragging中),(2)用户点击UIButtons从一个单元格导航到另一个单元格,(3)用户点击UIButton在数组的末尾创建并附加一个新的单元格(用于通过collectionView)。

I have created a method to detect the current cell, which I call every time in any of these cases: (1) user scrolls from one cell to another (method placed in scrollViewWillEndDragging), (2) user taps UIButtons to navigate from one cell to another, (3) user taps UIButton to create and append a new cell at the end of the array (which is used by the collectionView).

这是方法:

func setNewFirstResponder() {
    let currentIndex = IndexPath(item: currentCardIndex, section: 0)
    if let newCell = collectionView.cellForItem(at: currentIndex) as? AddCardCell {
        newCell.questionTextView.becomeFirstResponder()
    }
}



Now my problem is that this only works in case (1). Apparently I have no cell of type AddCardCell in cases (2) and (3). When I print the currentCardIndex, I get the same result, in all of the cases, which is very confusing.

有人暗示为什么在情况2和3中我还无法获得单元格,但在情况1中我还是可以?

Any hints why I wouldn't be able to get the cell yet in cases 2 and 3, but I am in case 1?

作为参考,这里是我使用的一些方法:

As a reference here are some of the methods that I am using:

//Update index and labels based on user swiping through card cells
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    //Update index based on targetContentOffset
    let x = targetContentOffset.pointee.x
    currentCardIndex = Int(x / view.frame.width)

    setNewFirstResponder()
}

另一种方法无法使用(案例3):

And the other method, from which it doesn't work (case 3):

//Method to add a new cell at end of collectionView
@objc func handleAddCell() {
    //Inserting a new index path into tableView
    let newIndexPath = IndexPath(item: autoSaveCards.count - 1, section: 0)
    collectionView.insertItems(at: [newIndexPath])

    collectionView.scrollToItem(at: newIndexPath, at: .left, animated: true)
    currentCardIndex = autoSaveCards.count - 1

    setNewFirstResponder()
}


推荐答案

关于情况2,3,我认为单元尚未加载,因此如果让失败,您可以尝试在这样的一段时间后分派,如果单元格不可见,则为空

Regarding case 2,3 i think that the cell is not yet loaded so if let fails , you can try to dispatch that after some time like this , also general note if the cell is not visible then it's nil

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
   setNewFirstResponder()
}

如果设置 animated:false 到 scrollToItem

这篇关于导航单元格时,使collectionView单元格中的textView成为firstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:37