编辑:我不知道为什么投票不足,但我有这段代码在一个应用程序中工作,将单元格相互合并。我本来打算上传一个视频来展示正在做的事情,但现在我不会着急,抱歉。
它没有委托方法,我在一个应用程序中做了一些工作。我是这样做的。使用 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.
}
*/
}
使用此代码,如果将被覆盖的单元格放在其他单元格上,您将能够合并单元格的内容。问题是如何区分拖放单元格或合并两个单元格。您需要为此添加一些逻辑,或者让用户选择如何使用选择器,我的意思是,在编辑单元格之前,用户可以选择移动或合并单元格,这样您就会知道该怎么做。
其他选项是添加自定义拖动按钮,或在单元格上添加长按等...以确定您是拖动还是合并。