单元重复以及数据改组在tableview处于isEditing状态时编辑文本字段以及移动行时发生

问题描述

因此,我有一个屏幕,其中添加了存储在数组“ arrayToUse”中的练习。 image here

当我按屏幕顶部的编辑时,表格视图变为可编辑状态。集合和代表的标签将转换为文本字段,因此用户也可以编辑这些值。

当我为某个练习更改集合和代表的值并在那之后交换行时,问题就出现了。单元格重复。 image here

按下“完成”后,单元格将变得不可编辑,这就是创建第二个问题的位置,其中将集合和代表的数据添加到另一个练习中。 image here

这是cellForRowAt的代码

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "notesCell",for: indexPath) as! NotesCell
    cell.accessoryType = .none
            cell.notesTextView.layer.borderWidth = 2
            cell.notesTextView.layer.borderColor = UIColor(red: 110/255,green: 146/255,blue: 204/255,alpha: 1).cgColor
            cell.notesTextView.clipsToBounds = true
            cell.notesTextView.layer.cornerRadius = 15
      return cell
  } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "inputCell",for: indexPath) as! TableViewCell
            cell.workoutLabel.text = arrayToUse[indexPath.section - 1].subName
            let setValue = arrayToUse[indexPath.section - 1].set
            cell.setsTextField.text = setValue
            cell.setsValueLabel.text = setValue
            cell.setsTextField.isHidden = !ttableView.isEditing
            let repValue = arrayToUse[indexPath.section - 1].rep
            cell.repsTextField.text = repValue
            cell.repsValueLabel.text = repValue
            cell.repsTextField.isHidden = !ttableView.isEditing
            
            cell.setsValueLabel.isHidden = ttableView.isEditing
            cell.repsValueLabel.isHidden = ttableView.isEditing
            print("Sets: \(setValue) Reps: \(repValue)")
            cell.delegate = self
            
            cell.setsTextField.layer.cornerRadius = 5
            cell.setsTextField.layer.borderWidth = 1.5
            cell.setsTextField.layer.borderColor = UIColor(red: 104/255,green: 135/255,blue: 189/255,alpha: 1).cgColor
           
            cell.repsTextField.layer.cornerRadius = 5
            cell.repsTextField.layer.borderWidth = 1.5
            cell.repsTextField.layer.borderColor = UIColor(red: 104/255,alpha: 1).cgColor
            
                return cell
        }
    }

这是moveRowAt的代码

override func tableView(_ tableView: UITableView,moveRowAt sourceIndexPath: IndexPath,to destinationIndexPath: IndexPath) {
        let moveSetsData = arrayToUse[sourceIndexPath.section-1]
        arrayToUse.remove(at: sourceIndexPath.section-1)
        arrayToUse.insert(moveSetsData,at: destinationIndexPath.section-1)
    }

这是由barButton执行的功能代码

 @objc func rightBarButtonItemTapped(_ sender: UIBarButtonItem) {
        ttableView.setEditing(!ttableView.isEditing,animated: true)
        navigationItem.rightBarButtonItems![1].title = ttableView.isEditing ? "Done" : "Edit"
        navigationItem.rightBarButtonItems![1].style = ttableView.isEditing ? .done : .plain
        var counter = 0
        
        ttableView.visibleCells.forEach { cell in
            guard let cell = cell as? TableViewCell else { return }
            cell.setsTextField.isHidden = !ttableView.isEditing
            cell.repsTextField.isHidden = !ttableView.isEditing
            
            cell.setsValueLabel.isHidden = ttableView.isEditing
            cell.repsValueLabel.isHidden = ttableView.isEditing
           
            cell.setsValueLabel.text = cell.setsTextField.text
            cell.repsValueLabel.text = cell.repsTextField.text
           
            let setsValue = cell.setsTextField.text!
            arrayToUse[counter].set = setsValue
            let repsValue = cell.repsTextField.text!
            arrayToUse[counter].rep = repsValue
        
            counter += 1
        }
        view.endEditing(true)
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)