UI手势长按复制单元格
Posted
技术标签:
【中文标题】UI手势长按复制单元格【英文标题】:UIGesture LongPress duplicating cells 【发布时间】:2015-10-31 21:15:55 【问题描述】:在我的 tableview 单元格的 UI 中遇到了一个奇怪的交互缺陷。我实现了一个长按手势:
func handleLongPress(sender:UILongPressGestureRecognizer!)
var myCharacters: SelectedCharacter?
let localLongPress = sender as UILongPressGestureRecognizer
let locationInView = localLongPress.locationInView(cardsListed)
let indexPath = charactersListed.indexPathForRowAtPoint(locationInView)
let listed = frc.objectAtIndexPath(indexPath!) as! Characters
let cell: firstCharacterDetails = charactersListed.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as! firstCharacterDetails
if listed == 0
else
if (sender.state == UIGestureRecognizerState.Ended)
print("Long press Ended")
else if (sender.state == UIGestureRecognizerState.Began)
let bounds: CGRect = UIScreen.mainScreen().bounds
let screenHeight: NSNumber = bounds.size.height
if screenHeight == 480
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let characterDetailsController: CharacterDetailsViewController = storyboard.instantiateViewControllerWithIdentifier("characterDetails") as! CharacterDetailsViewController
characterDetailsController.modalPresentationStyle = UIModalPresentationStyle.Popover
let popoverplayersCharacterController = characterDetailsController.popoverPresentationController
popoverCharacterNoteController?.permittedArrowDirections = .Any
popoverCharacterNoteController?.delegate = self
popoverCharacterNoteController?.sourceView = cell.cardDescription
characterDetailsController.characterDetails = listed
self.presentViewController(characterDetailsController, animated: true, completion: nil)
如果编码不完整,这是一个示例,抱歉,因为我刚刚取出该代码区域以供参考。现在我的问题是一个非常独特的问题,并且仅可能发生在用户的意外手势上,但为了用户的安全,我想删除这个问题。
我面临的问题是,当用户长按手势选定的单元格时,如果他们不小心用手指拖动到另一个单元格同时仍然握住该单元格,实际上会复制自身或将自身拖动到用户长按手势拖动的单元格下方到。我不确定如何处理以防止这种情况发生,但如果有人有任何见解,将不胜感激!
【问题讨论】:
【参考方案1】:发现我做错了什么。在第二次查看代码后,我意识到调用 dequeueResuableCell 是不正确的函数。我用以下代码切换代码行:
let cell: firstCharacterDetails = charactersListed.cellForRowAtIndexPath(indexPath!) as! firstCharacterDetails
问题解决了。当我复制并粘贴一些我在几个不同控制器中重复使用的代码时,我并没有考虑太多。
【讨论】:
以上是关于UI手势长按复制单元格的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 单元格按钮长按手势返回自我不断变化?