tableView.addGestureRecognizer 用于 tableViewController 的一部分 [英] tableView.addGestureRecognizer for one section of the tableViewController
问题描述
我是 iOS 新手,很快.我的 tableView 中有两个部分.我希望能够在第二部分而不是第一部分上执行 longPressGesture,使用户能够重新排序第二部分中的 tableview 单元格.我将如何快速做到这一点?谁能提供一个简单的 Swift 示例代码?
I'm new to iOS, swift. I have two sections in my tableView. I want to be able to do a longPressGesture on the second section, and not the first, enabling the user to reorder tableview cells in the second section. How would I do that in swift? Would anyone kindly provide a simple sample code in Swift?
感谢您的帮助,非常感谢!
Thanks for your help, much appreciated!
推荐答案
如果您只想为特定的移动单元重新排序,您可以添加一些按钮/操作来启用/禁用重新排序,您可以使用委托
If you just want to reorder move the cell for the particular you may add some button/action to enable/disable reorder , there is delegate which you can use
你的代码可以是这样的:
Your code can be like this:
//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
委托方法可以这样使用:
The delegate methods can be use like this:
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.section == 0 {返回假} 别的 {返回真}//如果您不希望该项目可重新排序,则返回 false.}
//write code to allow reorder in the particular section/indexpath if indexPath.section == 0 { return false } else { return true } // Return false if you do not want the item to be re-orderable. }
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 cell上实现
the UILongPressGestureRecognizer can be implemented on the tableview or the tableview cell based on the requirements
let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized))
tblView.addGestureRecognizer(longpress)
func longPressGestureRecognized() {
NSLog("Detected")
tblView.editing = true
}
或在tableview单元格中使用与上述相同的方法
or in tableview cell with same method as above
let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized))
cell.addGestureRecognizer(longpress)
这篇关于tableView.addGestureRecognizer 用于 tableViewController 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!