tableView.addGestureRecognizer 用于 tableViewController 的一部分
tableView.addGestureRecognizer for one section of the tableViewController
我是 iOS、swift 的新手。我的 tableView 中有两个部分。我希望能够在第二部分而不是第一部分执行 longPressGesture,使用户能够在第二部分中重新排序 tableview 单元格。我将如何在 swift 中做到这一点?谁能在Swift中提供一个简单的示例代码?
感谢您的帮助,非常感谢!
如果您只想重新排序移动特定的单元格,您可以添加一些 button/action 到 enable/disable 重新排序,您可以使用委托
你的代码可以是这样的:
//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 {
//编写代码以允许在特定 section/indexpath 中重新排序
如果 indexPath.section == 0 {
return 错误
} 别的 {
return 是的
}
// Return 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 cell上
let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized))
tblView.addGestureRecognizer(longpress)
func longPressGestureRecognized() {
NSLog("Detected")
tblView.editing = true
}
或在表格视图单元格中使用与上述相同的方法
let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized))
cell.addGestureRecognizer(longpress)
我是 iOS、swift 的新手。我的 tableView 中有两个部分。我希望能够在第二部分而不是第一部分执行 longPressGesture,使用户能够在第二部分中重新排序 tableview 单元格。我将如何在 swift 中做到这一点?谁能在Swift中提供一个简单的示例代码?
感谢您的帮助,非常感谢!
如果您只想重新排序移动特定的单元格,您可以添加一些 button/action 到 enable/disable 重新排序,您可以使用委托
你的代码可以是这样的:
//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 {
//编写代码以允许在特定 section/indexpath 中重新排序 如果 indexPath.section == 0 { return 错误 } 别的 { return 是的 } // Return 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 cell上
let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized))
tblView.addGestureRecognizer(longpress)
func longPressGestureRecognized() {
NSLog("Detected")
tblView.editing = true
}
或在表格视图单元格中使用与上述相同的方法
let longpress = UILongPressGestureRecognizer(target:self, action:#selector(HomeScreenTableViewController.longPressGestureRecognized))
cell.addGestureRecognizer(longpress)