tableView.addGestureRecognizer为tableViewController的一个部分

我是新来的iOS,迅速。 我有两个部分在我的tableView。 我希望能够在第二部分做一个longPressGesture,而不是第一部分,使用户能够在第二部分重新排列tableview单元格。 我将如何迅速做到这一点? 有谁愿意提供一个简单的示例代码在Swift中吗?

感谢您的帮助,非常感谢!

如果你只是想重新订购移动的细胞为特定的你可以添加一些button/动作来启用/禁用重新sorting,有代表,你可以使用

你的代码可以是这样的:

 //enable editing in the tableview to true when you want to enable reorder in your case may on the UILongPressGestureRecognizer action //In viewDidLoad() tblView.editing = true//set it to false to complete the reorder 

委托方法可以像这样使用:

 func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle.None } func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { //get the reorder change in the path, you can do operation on the array let itemToMove:String = arrData[fromIndexPath.row]//get the old path of item arrData.removeAtIndex(fromIndexPath.row)//remove item from old path arrData.insert(itemToMove, atIndex: toIndexPath.row)//at item at new path in array } func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 

//编写代码以允许在特定部分/ indexpath中重新sortingif indexPath.section == 0 {return false} else {return true} //如果不希望项目是可重新订购的,则返回false。 }

 func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath { //check if the reorder is allow in the particular section/indexpath before the reorder is done, return the old path if you don't want to move at Proposed path if sourceIndexPath.section != proposedDestinationIndexPath.section { return sourceIndexPath } else { return proposedDestinationIndexPath } } 

UILongPressGestureRecognizer可以根据需要在tableview或tableview单元格上实现

 let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized)) tblView.addGestureRecognizer(longpress) func longPressGestureRecognized() { NSLog("Detected") tblView.editing = true } 

或者使用与上面相同的方法在tableview单元格中

 let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized)) cell.addGestureRecognizer(longpress)