Как переместить строки в UITableView, изменяя даже раздел - PullRequest
0 голосов
/ 04 мая 2020

Мне нужно переместить некоторую строку в другой раздел, скажем, чтобы установить, находится ли какой-либо объект внутри списка или другой (вход и выход). если список «out» пуст, я покажу определенную ячейку, иначе я покажу нормальную. Я протестировал некоторые решения, но с моим кодом у меня возникли две проблемы: элементы во втором массиве не заполняют таблицу, и я не могу следить за сечением при перемещении.

    @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
    //    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...