在isEditing模式下的Swift Tableview不会取消选择单元格

时间:2020-05-24 14:05:17

标签: swift uitableview

我尝试在isEditing模式下使用标准选择。当我第一次按下时,它被选中,但是当我第二次按下时,它在视觉上保持选中状态。如果我将isEditing更改为false并在下次将其设置为true,则无法选择之前选择的行,但不会标记。

如何解决? isEditing按按钮更改。 enter image description here ''' PermanentInternalViewController类:UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIGestureRecognizerDelegate {

var index = 0
var test = ["dghffhfh",
            "sadasdsa",
            "sgfhghgfh"

]
var selectedArray: [IndexPath] = []
@IBOutlet weak var tableViewInternal: UITableView!
@IBOutlet weak var createButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(tableTapped))
    self.tableViewInternal.addGestureRecognizer(tap)


    self.tableViewInternal.delegate = self
    self.tableViewInternal.dataSource = self
    self.tableViewInternal.tableFooterView = UIView()
    self.tableViewInternal.allowsMultipleSelectionDuringEditing = true
    createButton.layer.cornerRadius = 10
    if tableViewInternal.isEditing {
        createButton.titleLabel!.text = "Add to your Temporary List"
    } else {
        createButton.titleLabel!.text = "Create Temporary List"
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    test.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "permanentInsideCell1", for: indexPath) as! customCell

    cell.tf.delegate = self
    //        cell.selectionStyle = .none
    cell.tf.text = test[indexPath.row]
    cell.tf.isUserInteractionEnabled = false






    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! customCell
    cell.tf.isUserInteractionEnabled = true
    cell.selectionStyle = .none
    cell.tf.becomeFirstResponder()
    index = indexPath.row

    if tableViewInternal.isEditing{
        cell.tf.isUserInteractionEnabled = false
    }
    print (cell.isSelected)

    if  selectedArray.contains(indexPath) {
        // it was already selected
        selectedArray.remove(at: selectedArray.firstIndex(of: indexPath)!)


        print(selectedArray)
    } else {
        // wasn't yet selected, so let's remember it
        selectedArray.append(indexPath)

        print(selectedArray)
    }
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    print("Deselect")

}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
        test.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
    test[index] = textField.text!
    tableViewInternal.reloadData()
    //        textField.resignFirstResponder()
}

@IBAction func createTempList(_ sender: UIButton) {
    tableViewInternal.isEditing = true
    createButton.setTitle("Add to your Temporary List", for: .normal)



}
func addAction() {
    // create a new row by appending new empty strings
    test.append("")

    tableViewInternal.reloadData()
}

@objc func tableTapped(tap:UITapGestureRecognizer) {
    tap.cancelsTouchesInView = false
    let location = tap.location(in: self.tableViewInternal)
    let path = self.tableViewInternal.indexPathForRow(at: location)
    if let _ = path {

        //            self.tableView(self.tableViewInternal, didSelectRowAt: indexPathForRow)
    } else {
        // handle tap on empty space below existing rows however you want
        if self.tableViewInternal.isEditing {
            self.tableViewInternal.isEditing = false
            self.createButton.setTitle("Create Temporary List", for: .normal)
        } else {
            addAction()
        }
    }
}

}

'''

1 个答案:

答案 0 :(得分:0)

已解决。 问题出在cell.selectionStyle = .none 如果将其删除并为表视图使用清晰的背景色,则一切正常。

相关问题