How to move rows in UITableView changing even the section

谁都会走 提交于 2021-02-11 12:29:04

问题


I need to move some row to a different section, let's say to set if some object is inside a list or another (in and out). if the "out" list is empty I'll show a certain cell, otherwise I'll show the normal one. I tested some solutions, but with my code I have two problems, the items in second array are not populating the table, and I cannot keep trace of section while moving.

    @IBOutlet weak var testTable: UITableView!

    var globalArray = [["test1", "test2", "test3"], ["out dummy item"]]
    let kCellId = "CustomTestCell"
    let headerTitles = ["in", "out"]


    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "my test"

        testTable.delegate = self
        testTable.dataSource = self

//        //required for moving
//        self.testTable.isEditing = true

    }

}


//MARK: tableView section

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

           //getting the index path of selected row
           let indexPath = tableView.indexPathForSelectedRow
           //getting the current cell from the index path
           guard let currentCell = tableView.cellForRow(at: indexPath!) as? CustomTestCell else {
               print("ERROR no current cell")
               return
           }
           //getting the text of that cell
           let currentItem = currentCell.testLabel?.text

           print(currentItem!)
        print(indexPath!)

       }

    //dataSource


        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            print("sections:", globalArray.count)
            return globalArray.count
        }


        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return headerTitles[section]
        }


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return globalArray[section].count

        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: kCellId, for: indexPath) as! CustomTestCell
            cell.testLabel.text = self.globalArray[indexPath.section][indexPath.row]

            return cell

        }


    //    //required for moving
    //    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    //        return .none
    //    }
    //
    //    //required for moving
    //    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    //        return false
    //    }
    //
    //
    //    //required for moving
    //    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    //        let movedObject = self.globalArray[sourceIndexPath.row]
    //        globalArray.remove(at: sourceIndexPath.row)
    //        globalArray.insert(movedObject, at: destinationIndexPath.row)
    //        print(globalArray) //and save on userdefaults
    //    }

来源:https://stackoverflow.com/questions/61587806/how-to-move-rows-in-uitableview-changing-even-the-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!