2017-01-24 20 views
0

我有一個UICollectionView,每個單元格都有一個UITextView。我試圖在長按TextView中的文本並從上下文菜單中選擇「刪除」時刪除整個單元格。獲取哪個UIcollectionviewcell觸摸的文本視圖是

我知道我可以覆蓋重寫

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) 
} 

我只是不知道如何找到當前單元格我英寸

+0

您可以通過使用indexPath.row爲UITextView的分配標籤,並通過這個這個標籤獲得的細胞。 – nynohu

+0

但是,如何知道上下文菜單中的哪個文本視圖? –

+0

在'cellForItemAtIndexPath'中,設置'cell.textView.tag = indexPath.row'。在func中刪除(_ sender:Any?){讓textView:UITextView = sender as! UITextView的; indexPath.row = textView.tag。只要調用delete collectionCell方法}' – nynohu

回答

0

斯威夫特3解決方案: 所以有些人很親密,但每個人都讓我走近了正確的道路。該解決方案並不完全回答我的問題,但可以結合使用gesturerecognizer來專門回答問題。

所以我的解決方案是基於我的代碼是如何格式化的,可能需要對其他人進行調整。

第一條: 從UICollectionViewCell子類中,我的textview被實現當然,我覆蓋了內置的刪除函數。從那裏創建一個變量,通過細胞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*/} 
-1

FUNC deleteSections( IndexSet) 刪除指定索引處的部分。

FUNC deleteItems(在:[IndexPath]) 刪除指定索引路徑

+0

但是我怎樣才能得到indexPath。我嘗試從覆蓋func刪除(_ sender:Any?){} –

+0

您的發件人是您的索引路徑。 [indexPath.row] – mrabins

+0

這裏的發件人是UIMenuController –

0

的項目在你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) 

我希望這會幫助你,或遇到其他任何問題,讓我知道:)

0

給標籤文本框即應在將細胞添加到收集視圖時等於當前indexPath。當長按監聽器被觸發時,檢查標籤值並在此基礎上進行操作。