UITableView拖放一个单元格到另一个单元格?

时间:2020-10-02 18:27:14

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

我有一个设置了拖放委托的UITableView。我可以在UITableView的其他单元格之间拖动单元格,但似乎没有委托方法可以将单元格拖动到另一个单元格的顶部,在这种情况下,我想将两个单元格合并(例如在iOS中拖动应用程序)。我似乎找不到在一个单元格放在另一个单元格顶部时调用的委托方法。什么时候检测某个单元何时已“掉落”到另一个单元之上的最佳方法是什么?谢谢

1 个答案:

答案 0 :(得分:0)

没有委托方法,我在有一些解决方法的应用程序中做到了。我是这样做的。使用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.
    }
    */

}

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

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