【问题标题】:UITableView drag and drop a cell onto another cell?UITableView 将一个单元格拖放到另一个单元格上?
【发布时间】:2020-10-02 18:27:14
【问题描述】:

我有一个设置了拖放委托的 UITableView。我能够在 UITableView 中的其他单元格之间拖动单元格,但是如果我将一个单元格拖动到另一个单元格顶部会发生什么,似乎没有委托方法,在这种情况下,我想组合这两个单元格(有点就像在 iOS 中拖动应用程序一样)。我似乎找不到当一个单元格放置在另一个单元格顶部时调用的委托方法。检测一个单元格何时“掉落”到另一个单元格上的最佳方法是什么?谢谢

【问题讨论】:

    标签: ios swift uitableview drag-and-drop uikit


    【解决方案1】:

    编辑:我不知道为什么投票不足,但我有这段代码在一个应用程序中工作,将单元格相互合并。我本来打算上传一个视频来展示正在做的事情,但现在我不会着急,抱歉。

    它没有委托方法,我在一个应用程序中做了一些工作。我是这样做的。使用 Swift 中的基本设置创建一个 UITableViewController:

    import UIKit
    
    class TableViewController: UITableViewController {
        
        var fruitsArray = ["Apple", "Banana", "Mango", "Guava", "PineApple", "Watermelon", "Grapes", "GroundNut", "Muskmelon", "Orange", "Cherry"]
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false
    
            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem
            
            // Setup tableview
            tableView.isEditing = true
            
        }
    
        // MARK: - Table view data source
    
        override func numberOfSections(in tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
            return fruitsArray.count
        }
    
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    
            // Configure the cell...
            cell.textLabel?.text = fruitsArray[indexPath.row]
    
            return cell
        }
    
        
        // Override to support conditional editing of the table view.
        override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            // Return false if you do not want the specified item to be editable.
            return true
        }
        
    
        
        // Override to support editing the table view.
        override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            if editingStyle == .delete {
                // Delete the row from the data source
                tableView.deleteRows(at: [indexPath], with: .fade)
            } else if editingStyle == .insert {
                // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
            }    
        }
    
        
        // Override to support rearranging the table view.
        override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
    
            if fromIndexPath.row != to.row {
                let sourceString = fruitsArray[fromIndexPath.row];
                let destinationString = fruitsArray[to.row];
                let mergeString = String("\(destinationString), \(sourceString)")
    
                fruitsArray[to.row] = mergeString //replaceObjectAtIndex:destinationIndexPath.row withObject:destinationString];
                fruitsArray.remove(at: fromIndexPath.row)
    
                tableView.reloadData()
            }
        }
    
        
        // Override to support conditional rearranging of the table view.
        override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
            // Return false if you do not want the item to be re-orderable.
            return true
        }
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    

    使用此代码,如果将被覆盖的单元格放在其他单元格上,您将能够合并单元格的内容。问题是如何区分拖放单元格或合并两个单元格。您需要为此添加一些逻辑,或者让用户选择如何使用选择器,我的意思是,在编辑单元格之前,用户可以选择移动或合并单元格,这样您就会知道该怎么做。

    其他选项是添加自定义拖动按钮,或在单元格上添加长按等...以确定您是拖动还是合并。

    【讨论】:

    • 我知道这是一篇相当老的帖子,但我找不到更新鲜的东西。您能否解释一下这段代码的哪一部分检测到一个单元格何时被“放置”在另一个单元格之上?我不认为 - 如果 fromIndexPath.row != to.row - 会检测到这一点,还是我错过了重点?
    • 嗨@grep“魔术”在:''override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath)''这个方法合并单元格并重新排列顺序中的总单元格。
    猜你喜欢
    • 2014-02-03
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多