gpt4 book ai didi

ios - tableView.addGestureRecognizer 用于 tableViewController 的一部分

转载 作者:可可西里 更新时间:2023-11-01 02:17:04 25 4
gpt4 key购买 nike

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

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

最佳答案

如果您只想重新排序移动特定的单元格,您可以添加一些按钮/操作来启用/禁用重新排序,您可以使用委托(delegate)

你的代码可以是这样的:

//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

委托(delegate)方法可以这样使用:

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。

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
}

或在表格 View 单元格中使用与上述相同的方法

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

关于ios - tableView.addGestureRecognizer 用于 tableViewController 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36820428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com