Search code examples
swifttablevieweditdelete-rowswiftui-tabview

Swipe and delete row with two sections in table view and add deleted name to second section


please help, I have an array with names and when I select a cell I add the selected name to the second section of a table view and delete the name from that first section (all fine here) but if I don't want the name in second section for some reason, I want to be able to swipe the cell, remove the name and add it again to the firs section of the table view. When I do that with my code the number of rows fails because I deleted a row. I can't figure it out.

here is my code.

import UIKit

class QuestionsVC: UIViewController {
    
    @IBOutlet weak var namesTableView: UITableView!

    var array1 = ["Jill","Clark","Rose","Peter","Louis"]
    var array2 = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        namesTableView.dataSource = self
        namesTableView.delegate = self
        
    }
}

extension QuestionsVC: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return array1.count
        }
        return array2.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
            cell.textLabel?.text = array1[indexPath.row]
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        
            cell?.textLabel?.text = array2[indexPath.row]
        
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        array2.append(array1[indexPath.row])
        if let index = array1.firstIndex(of: array1[indexPath.row]) {
            array1.remove(at: index)
        }
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if indexPath.section == 0 {
            return false
        }else{
            return true
        }
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (_, indexPath) in
            self.array2.remove(at: indexPath.row)
            self.array1 += [self.array2[indexPath.row]]
            
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
        return [deleteAction]
    }
}

Solution

  • You have to delete the row in section 1 and insert the row in section 0 simultaneously

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
            let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (_, indexPath) in
                let item = self.array2.remove(at: indexPath.row)
                let insertionIndex = self.array1.count
                self.array1.append(item)
                tableView.beginUpdates()
                tableView.deleteRows(at: [indexPath], with: .fade)
                tableView.insertRows(at: [IndexPath(row: insertionIndex, section: 0)], with: .automatic)
                tableView.endUpdates()
            }
            return [deleteAction]
        }
    }