Как создать пользовательский аксессуар галочки изображения для выбранных строк tableView? - PullRequest
0 голосов
/ 20 июня 2019

Как я могу назначить пользовательское изображение и для выбора и отмены .checkmark аксессуар tableView?

Можно ли также расположить этот пользовательский аксессуар с левой стороны строки tableView и постоянно видеть его в tableView?

При первой загрузке tableView аксессуар по-прежнему отображается, если он не выбран, по существу аналогично приложению Apple Reminders.

Вот пример того, что я ищу:

дезактивирует:

enter image description here

отмеченный:

enter image description here

В настоящее время это то, что у меня есть:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .none
}

1 Ответ

0 голосов
/ 20 июня 2019

Вот пример кода.Вам нужно создать свой собственный вид аксессуаров для моего случая. Я просто добавил один кружок внутри пользовательского вида. После этого вам просто нужно сделать скрытый true или false.

extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.accessoryView = CheckMarkView.init()
    cell.accessoryView?.isHidden = true
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    tableView.cellForRow(at: indexPath)?.accessoryView?.isHidden = false
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryView?.isHidden = true
}

}

class CheckMarkView: UIView {
override init(frame: CGRect) {
    super.init(frame: frame) // calls designated initializer
    let img = UIImage(named: "circle.png") //replace with your image name
    let imageView: UIImageView = UIImageView(image: img)
    self.addSubview(imageView)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...