获取我正在触摸的 textview 在哪个 UIcollectionviewcell 中
Getting which UIcollectionviewcell the textview im touching is in
我有一个 UICollectionView,每个单元格都有一个 UITextView。我试图在长按 TextView 中的文本并从上下文菜单中选择删除时删除整个单元格。
我知道我可以通过覆盖
来覆盖删除
func delete(_ sender: Any?) {//I want to delete cell from here}
和
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(delete(_:))
{
return true
}
return super.canPerformAction(action, withSender: sender)
}
我只是不知道如何找到我所在的当前单元格。
func deleteSections(IndexSet)
删除指定索引处的部分。
func deleteItems(位于:[IndexPath])
删除指定索引路径中的项目
在您的 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
中将 indexPath.row 设置为您的 UITextView 的标签。然后在您的 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
或 UITextView 中长按您可以访问 UITextView 的标签,这有助于找到当前单元格。
您可以使用此代码访问当前单元格:
let indexPath = IndexPath(row: textview.tag, section: 0)
let yourCell = self.collectionView.cellForItem(at: indexPath)
希望这对您有所帮助,或者您遇到任何其他问题,请告诉我:)
在将单元格添加到集合视图时,将标签赋予文本字段,该标签应等于当前的 indexPath。当触发长按监听器时,检查标签值,并根据该值进行操作。
Swift3 解决办法:
所以有些人很接近,但几乎每个人都带领我走上了正确的道路。该解决方案并不能完全回答我的问题,但它可以与手势识别器结合使用来专门回答问题。
所以我的解决方案是基于我的代码的格式化方式,可能需要针对其他人进行调整。
首先:
从 UICollectionViewCell 子类,我的 textview 当然是在其中实现的,我覆盖了内置的删除功能。从那里创建了一个变量,该变量用 cells superview 实例化,它是一个 UICollectionView。这允许我获取单元格的 indexPath。从那里我还创建了一个变量,该变量是用 UIView 实例化的,用于我的 UICollectionView 所在的位置。从那里我在我的 UIView 中创建了一个函数,它接受一个 IndexPath 并删除单元格。
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!)
}
UIView 内部:
func delCell(indexPath: IndexPath) {/*code for cell removal here*/}
我有一个 UICollectionView,每个单元格都有一个 UITextView。我试图在长按 TextView 中的文本并从上下文菜单中选择删除时删除整个单元格。
我知道我可以通过覆盖
来覆盖删除func delete(_ sender: Any?) {//I want to delete cell from here}
和
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(delete(_:))
{
return true
}
return super.canPerformAction(action, withSender: sender)
}
我只是不知道如何找到我所在的当前单元格。
func deleteSections(IndexSet) 删除指定索引处的部分。
func deleteItems(位于:[IndexPath]) 删除指定索引路径中的项目
在您的 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
中将 indexPath.row 设置为您的 UITextView 的标签。然后在您的 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
或 UITextView 中长按您可以访问 UITextView 的标签,这有助于找到当前单元格。
您可以使用此代码访问当前单元格:
let indexPath = IndexPath(row: textview.tag, section: 0)
let yourCell = self.collectionView.cellForItem(at: indexPath)
希望这对您有所帮助,或者您遇到任何其他问题,请告诉我:)
在将单元格添加到集合视图时,将标签赋予文本字段,该标签应等于当前的 indexPath。当触发长按监听器时,检查标签值,并根据该值进行操作。
Swift3 解决办法: 所以有些人很接近,但几乎每个人都带领我走上了正确的道路。该解决方案并不能完全回答我的问题,但它可以与手势识别器结合使用来专门回答问题。
所以我的解决方案是基于我的代码的格式化方式,可能需要针对其他人进行调整。
首先: 从 UICollectionViewCell 子类,我的 textview 当然是在其中实现的,我覆盖了内置的删除功能。从那里创建了一个变量,该变量用 cells superview 实例化,它是一个 UICollectionView。这允许我获取单元格的 indexPath。从那里我还创建了一个变量,该变量是用 UIView 实例化的,用于我的 UICollectionView 所在的位置。从那里我在我的 UIView 中创建了一个函数,它接受一个 IndexPath 并删除单元格。
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!)
}
UIView 内部:
func delCell(indexPath: IndexPath) {/*code for cell removal here*/}