简体   繁体   English

获取我正在触摸的textview所在的UIcollectionviewcell

[英]Getting which UIcollectionviewcell the textview im touching is in

I have a UICollectionView and each cell has a UITextView. 我有一个UICollectionView,每个单元都有一个UITextView。 I'm trying to make it possible to delete the entire cell when long pressing the text in a TextView and selecting Delete from the context menu. 我试图在长按TextView中的文本并从上下文菜单中选择Delete时删除整个单元格。

I know I can override Delete by overriding 我知道我可以通过覆盖来覆盖删除

func delete(_ sender: Any?) {//I want to delete cell from here}

and

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(delete(_:))
    {
        return true
    }
    return super.canPerformAction(action, withSender: sender)
}

I just don't know how to find the current cell I am in. 我只是不知道如何找到我所在的当前单元格。

In your func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell set indexPath.row as tag of your UITextView. 在您的func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell将indexPath.row设置为UITextView的标签。 Then in your func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) or UITextView long press you can access the tag of UITextView which helps to find the current cell. 然后在您的func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)或UITextView中长按即可访问UITextView的标记,该标记有助于查找当前单元格。

You can access current cell using this code: 您可以使用以下代码访问当前单元格:

let indexPath = IndexPath(row: textview.tag, section: 0)
let yourCell = self.collectionView.cellForItem(at: indexPath)

I hope this will help you, Or you are experiencing any other problems, let me know :) 希望对您有所帮助,或者您遇到任何其他问题,请告诉我:)

Give tag to textfield which should be equal to current indexPath while adding cell to collection view. 在将单元格添加到集合视图时,将标记提供给应与当前indexPath相等的文本字段。 When long press listener is triggered, check the tag value and based on that, do you operation. 触发长按监听器时,请检查标签值并据此进行操作。

Swift 3 Solution: So some people were close, but everyone led me down the right path almost. Swift 3解决方案:所以有些人很亲近,但每个人几乎都带领我走了正确的道路。 The solution doesn't exactly answer my question, but it could be combined with a gesturerecognizer to answer the question specifically. 该解决方案不能完全回答我的问题,但可以与手势识别器结合使用以专门回答问题。

So my solution is based on how my code is formatted and may need to be tweaked for anyone else. 因此,我的解决方案基于代码的格式,并且可能需要针对其他任何人进行调整。

First: From the UICollectionViewCell subclass, where my textview is implemented of course, I overrode the built in delete function. 首先:从UICollectionViewCell子类中,我的textview当然是在其中实现的,我覆盖了内置的delete函数。 From there is created a variable that is instantiated with with the cells superview, which is a UICollectionView. 从那里创建一个用单元格超级视图实例化的变量,它是一个UICollectionView。 This allows me to get the indexPath for the cell. 这使我可以获得该单元格的indexPath。 From there I also create a variable that is instantiated with the UIView for where my UICollectionView is located. 从那里,我还创建了一个变量,该变量用UIView实例化为我的UICollectionView所在的位置。 From there I created a function inside my UIView that takes in a IndexPath and deletes the cell. 从那里,我在UIView中创建了一个函数,该函数接受IndexPath并删除该单元格。

Inside UICollectionViewCell: 在UICollectionViewCell内部:

override func delete(_ sender: Any?) {
    let cv = self.superview as! UICollectionView
    let indexPath = cv.indexPath(for: self)
    let view = self.superview?.superview as! UIView
    view.delCell(indexPath!)
}

Inside UIView: 在UIView内部:

func delCell(indexPath: IndexPath) {/*code for cell removal here*/}

func deleteSections(IndexSet) Deletes the sections at the specified indexes. func deleteSections(IndexSet)删除指定索引处的节。

func deleteItems(at: [IndexPath]) Deletes the items at the specified index paths func deleteItems(at:[IndexPath])删除指定索引路径中的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM