Swift 4 - UITableViewController скрыть кнопку удаления editStyleForRowAt .delete - PullRequest
0 голосов
/ 14 мая 2019

У меня есть контроллер табличного представления, и я хочу, чтобы пользователь мог удалить элемент, поэтому я реализовал это:

 override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}

override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        self.array.remove(at: indexPath.row)
        tableView.reloadData()
    }
}

У меня вопрос, как мне скрыть кнопку удаления и показать ее, только если пользователь проведет пальцем по строке?

Вот как это выглядит, имеет ли значение, если я включил сортировку?

enter image description here

1 Ответ

0 голосов
/ 14 мая 2019

Попробуйте использовать это вместо тех трех функций, которые вы пробовали:

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

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        self.array.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }

    return [deleteAction]
}

EDIT

После просмотра вашего скриншота я считаю, что это то, чего вы пытаетесь достичь. Вы пытаетесь включить удаление, когда нажата editButton, а затем появляется значок удаления. Если это так, пожалуйста, попробуйте этот код.

class TableViewController: UITableViewController {
    var array = [1,2,3]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
         navigationItem.rightBarButtonItem = editButtonItem
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.setEditing(editing, animated: true)
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(array[indexPath.row])"
        return cell
    }

    override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        //deleting logic
    }
}

В противном случае, вы можете рассмотреть возможность реализации только слайда для удаления анимации, и код таков до EDIT .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...